procfs.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-07-07 GuEe-GUI first version
  9. */
  10. #include <rtdef.h>
  11. #include <drivers/byteorder.h>
  12. #define DBG_TAG "pci.procfs"
  13. #define DBG_LVL DBG_INFO
  14. #include <rtdbg.h>
  15. #include "procfs.h"
  16. #ifdef RT_USING_LWP
  17. #include <lwp_user_mm.h>
  18. #endif
  19. static struct rt_bus *pci_bus;
  20. static struct proc_dentry *pci_proc_dentry;
  21. rt_inline void copy_to_user(void *to, void *from, size_t size)
  22. {
  23. #ifdef RT_USING_LWP
  24. if (!lwp_put_to_user(to, from, size))
  25. #endif
  26. {
  27. rt_memcpy(to, from, size);
  28. }
  29. }
  30. rt_inline void copy_from_user(void *to, const void *from, size_t size)
  31. {
  32. #ifdef RT_USING_LWP
  33. if (!lwp_get_from_user(to, (void *)from, size))
  34. #endif
  35. {
  36. rt_memcpy(to, (void *)from, size);
  37. }
  38. }
  39. static void pci_pm_runtime_get(struct rt_pci_device *pdev, rt_ubase_t *out_flags)
  40. {
  41. *out_flags = pdev->pm_enabled;
  42. if (!pdev->pm_enabled)
  43. {
  44. rt_pci_pme_active(pdev, RT_TRUE);
  45. }
  46. }
  47. static void pci_pm_runtime_put(struct rt_pci_device *pdev, rt_ubase_t *flags)
  48. {
  49. if (!*flags)
  50. {
  51. rt_pci_pme_active(pdev, RT_FALSE);
  52. }
  53. }
  54. static ssize_t pci_read(struct dfs_file *file, void *buf, size_t count, off_t *ppos)
  55. {
  56. off_t pos = *ppos;
  57. size_t res = count;
  58. rt_ubase_t pm_flags;
  59. struct proc_dentry *dentry = file->vnode->data;
  60. struct rt_pci_device *pdev = dentry->data;
  61. pci_pm_runtime_get(pdev, &pm_flags);
  62. if ((pos & 1) && count)
  63. {
  64. rt_uint8_t val;
  65. rt_pci_read_config_u8(pdev, pos, &val);
  66. copy_to_user(buf, &val, sizeof(val));
  67. ++buf;
  68. ++pos;
  69. --count;
  70. }
  71. if ((pos & 3) && count > 2)
  72. {
  73. rt_uint16_t val;
  74. rt_pci_read_config_u16(pdev, pos, &val);
  75. val = rt_cpu_to_le16(val);
  76. copy_to_user(buf, &val, sizeof(val));
  77. buf += 2;
  78. pos += 2;
  79. count -= 2;
  80. }
  81. while (count >= 4)
  82. {
  83. rt_uint32_t val;
  84. rt_pci_read_config_u32(pdev, pos, &val);
  85. val = rt_cpu_to_le32(val);
  86. copy_to_user(buf, &val, sizeof(val));
  87. buf += 4;
  88. pos += 4;
  89. count -= 4;
  90. }
  91. if (count >= 2)
  92. {
  93. rt_uint16_t val;
  94. rt_pci_read_config_u16(pdev, pos, &val);
  95. val = rt_cpu_to_le16(val);
  96. copy_to_user(buf, &val, sizeof(val));
  97. buf += 2;
  98. pos += 2;
  99. count -= 2;
  100. }
  101. if (count)
  102. {
  103. rt_uint8_t val;
  104. rt_pci_read_config_u8(pdev, pos, &val);
  105. copy_to_user(buf, &val, sizeof(val));
  106. ++pos;
  107. }
  108. pci_pm_runtime_put(pdev, &pm_flags);
  109. *ppos = pos;
  110. return res;
  111. }
  112. static ssize_t pci_write(struct dfs_file *file, const void *buf, size_t count, off_t *ppos)
  113. {
  114. off_t pos = *ppos;
  115. size_t res = count;
  116. rt_ubase_t pm_flags;
  117. struct proc_dentry *dentry = file->vnode->data;
  118. struct rt_pci_device *pdev = dentry->data;
  119. pci_pm_runtime_get(pdev, &pm_flags);
  120. if ((pos & 1) && count)
  121. {
  122. rt_uint8_t val;
  123. copy_from_user(&val, buf, sizeof(val));
  124. rt_pci_write_config_u8(pdev, pos, val);
  125. ++buf;
  126. ++pos;
  127. --count;
  128. }
  129. if ((pos & 3) && count > 2)
  130. {
  131. rt_le16_t val;
  132. copy_from_user(&val, buf, sizeof(val));
  133. rt_pci_write_config_u16(pdev, pos, rt_le16_to_cpu(val));
  134. buf += 2;
  135. pos += 2;
  136. count -= 2;
  137. }
  138. while (count >= 4)
  139. {
  140. rt_le32_t val;
  141. copy_from_user(&val, buf, sizeof(val));
  142. rt_pci_write_config_u32(pdev, pos, rt_le32_to_cpu(val));
  143. buf += 4;
  144. pos += 4;
  145. count -= 4;
  146. }
  147. if (count >= 2)
  148. {
  149. rt_le16_t val;
  150. copy_from_user(&val, buf, sizeof(val));
  151. rt_pci_write_config_u16(pdev, pos, rt_le16_to_cpu(val));
  152. buf += 2;
  153. pos += 2;
  154. count -= 2;
  155. }
  156. if (count)
  157. {
  158. rt_uint8_t val;
  159. copy_from_user(&val, buf, sizeof(val));
  160. rt_pci_write_config_u8(pdev, pos, val);
  161. ++pos;
  162. }
  163. pci_pm_runtime_put(pdev, &pm_flags);
  164. *ppos = pos;
  165. return res;
  166. }
  167. static off_t pci_lseek(struct dfs_file *file, off_t offset, int wherece)
  168. {
  169. struct proc_dentry *dentry = file->vnode->data;
  170. struct rt_pci_device *pdev = dentry->data;
  171. switch (wherece)
  172. {
  173. case SEEK_SET:
  174. break;
  175. case SEEK_CUR:
  176. offset += file->fpos;
  177. break;
  178. case SEEK_END:
  179. offset += pdev->cfg_size;
  180. break;
  181. default:
  182. return -EINVAL;
  183. }
  184. if (offset <= (off_t)pdev->cfg_size)
  185. {
  186. return offset;
  187. }
  188. return -EIO;
  189. }
  190. static const struct dfs_file_ops pci_fops =
  191. {
  192. .read = pci_read,
  193. .write = pci_write,
  194. .lseek = pci_lseek,
  195. };
  196. void pci_procfs_attach(struct rt_pci_device *pdev)
  197. {
  198. const char *name;
  199. struct proc_dentry *dentry;
  200. if (!pci_proc_dentry)
  201. {
  202. return;
  203. }
  204. name = rt_dm_dev_get_name(&pdev->parent);
  205. dentry = proc_create_data(name, 0644, pci_proc_dentry, &pci_fops, pdev);
  206. if (!dentry)
  207. {
  208. LOG_E("Create %s file fail", name);
  209. return;
  210. }
  211. proc_release(dentry);
  212. }
  213. void pci_procfs_detach(struct rt_pci_device *pdev)
  214. {
  215. if (!pci_proc_dentry)
  216. {
  217. return;
  218. }
  219. proc_remove_dentry(rt_dm_dev_get_name(&pdev->parent), pci_proc_dentry);
  220. }
  221. static int pci_single_show(struct dfs_seq_file *seq, void *data)
  222. {
  223. struct rt_device *dev;
  224. struct rt_pci_driver *pdrv;
  225. struct rt_pci_device *pdev;
  226. rt_hw_spin_lock(&pci_bus->dev_lock.lock);
  227. rt_list_for_each_entry(dev, &pci_bus->dev_list, node)
  228. {
  229. pdev = rt_container_of(dev, struct rt_pci_device, parent);
  230. dfs_seq_printf(seq, "%02x%02x\t%04x%04x\t%x",
  231. pdev->bus->number,
  232. pdev->devfn,
  233. pdev->vendor,
  234. pdev->device,
  235. pdev->irq);
  236. /* BAR, ROM base */
  237. for (int bar = 0; bar < RT_PCI_BAR_NR_MAX; ++bar)
  238. {
  239. dfs_seq_printf(seq, "\t%16llx", (rt_uint64_t)pdev->resource[bar].base);
  240. }
  241. dfs_seq_printf(seq, "\t%16llx", (rt_uint64_t)pdev->rom.base);
  242. /* BAR, ROM size */
  243. for (int bar = 0; bar < RT_PCI_BAR_NR_MAX; ++bar)
  244. {
  245. dfs_seq_printf(seq, "\t%16llx", (rt_uint64_t)pdev->resource[bar].size);
  246. }
  247. dfs_seq_printf(seq, "\t%16llx", (rt_uint64_t)pdev->rom.size);
  248. dfs_seq_puts(seq, "\t");
  249. /* Driver Name */
  250. if (dev->drv)
  251. {
  252. pdrv = rt_container_of(dev->drv, struct rt_pci_driver, parent);
  253. dfs_seq_puts(seq, pdrv->name);
  254. }
  255. /* End of a seq */
  256. dfs_seq_puts(seq, "\n");
  257. }
  258. rt_hw_spin_unlock(&pci_bus->dev_lock.lock);
  259. return 0;
  260. }
  261. static int pci_procfs_init(void)
  262. {
  263. struct proc_dentry *dentry;
  264. pci_bus = rt_bus_find_by_name("pci");
  265. RT_ASSERT(pci_bus != RT_NULL);
  266. pci_proc_dentry = proc_mkdir("pci", RT_NULL);
  267. if (!pci_proc_dentry)
  268. {
  269. LOG_E("Create pci entry fail");
  270. return (int)-RT_ERROR;
  271. }
  272. proc_release(pci_proc_dentry);
  273. dentry = proc_create_single_data("devices", 0644, pci_proc_dentry, &pci_single_show, NULL);
  274. if (!dentry)
  275. {
  276. proc_remove(pci_proc_dentry);
  277. pci_proc_dentry = RT_NULL;
  278. LOG_E("Create pci devices fail");
  279. return (int)-RT_ERROR;
  280. }
  281. proc_release(dentry);
  282. return 0;
  283. }
  284. INIT_PREV_EXPORT(pci_procfs_init);