lowl-pci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Copyright 2014 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #if defined(USE_VFIO) || defined(USE_UIO)
  11. # define USE_LOWL_PCI_MMAP
  12. # define USE_LOWL_PCI_DATA_WRAPPERS
  13. #endif
  14. #include <stdbool.h>
  15. #include <stddef.h>
  16. #include <stdint.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/types.h>
  21. #include <dirent.h>
  22. #include <sys/stat.h>
  23. #include <fcntl.h>
  24. #include <unistd.h>
  25. #include <utlist.h>
  26. #ifdef USE_VFIO
  27. #include <linux/vfio.h>
  28. #include <sys/ioctl.h>
  29. #endif
  30. #ifdef USE_LOWL_PCI_MMAP
  31. #include <sys/mman.h>
  32. #endif
  33. #include "logging.h"
  34. #include "lowlevel.h"
  35. #include "lowl-pci.h"
  36. #include "miner.h"
  37. #include "util.h"
  38. static
  39. struct lowlevel_device_info *pci_devinfo_scan()
  40. {
  41. struct lowlevel_device_info *devinfo_list = NULL, *info;
  42. struct dirent *de;
  43. char filename[0x100] = "/sys/bus/pci/devices", buf[0x10];
  44. DIR * const D = opendir(filename);
  45. if (!D)
  46. return 0;
  47. char * const p = &filename[strlen(filename)], *devid;
  48. const size_t psz = sizeof(filename) - (p - filename);
  49. uint32_t vid, pid;
  50. size_t d_name_len;
  51. while ( (de = readdir(D)) )
  52. {
  53. d_name_len = strlen(de->d_name);
  54. snprintf(p, psz, "/%s/vendor", de->d_name);
  55. if (!bfg_slurp_file(buf, sizeof(buf), filename))
  56. continue;
  57. vid = strtoll(buf, NULL, 0);
  58. snprintf(p, psz, "/%s/device", de->d_name);
  59. if (!bfg_slurp_file(buf, sizeof(buf), filename))
  60. continue;
  61. pid = strtoll(buf, NULL, 0);
  62. devid = malloc(4 + d_name_len + 1);
  63. sprintf(devid, "pci:%s", de->d_name);
  64. info = malloc(sizeof(struct lowlevel_device_info));
  65. *info = (struct lowlevel_device_info){
  66. .lowl = &lowl_pci,
  67. .devid = devid,
  68. .path = strdup(de->d_name),
  69. .vid = vid,
  70. .pid = pid,
  71. };
  72. LL_PREPEND(devinfo_list, info);
  73. }
  74. closedir(D);
  75. return devinfo_list;
  76. }
  77. struct lowl_pci_interface {
  78. struct lowl_pci_handle *(*open)(const char *path, const struct _lowl_pci_config *);
  79. void (*close)(struct lowl_pci_handle *);
  80. const uint32_t *(*get_words)(struct lowl_pci_handle *, void *buf, size_t words, int bar, off_t);
  81. const void *(*get_data)(struct lowl_pci_handle *, void *buf, size_t, int bar, off_t);
  82. bool (*set_words)(struct lowl_pci_handle *, const uint32_t *, size_t, int bar, off_t);
  83. bool (*set_data)(struct lowl_pci_handle *, const void *, size_t, int bar, off_t);
  84. };
  85. struct lowl_pci_handle {
  86. const char *path;
  87. const struct lowl_pci_interface *lpi;
  88. #ifdef USE_VFIO
  89. int fd[3];
  90. off_t baroff[6];
  91. #endif
  92. #ifdef USE_LOWL_PCI_MMAP
  93. uint32_t *bar[6];
  94. size_t barsz[6];
  95. #endif
  96. };
  97. #ifdef USE_LOWL_PCI_MMAP
  98. static
  99. void lowl_pci_close_mmap(struct lowl_pci_handle * const lph)
  100. {
  101. for (int i = 0; i < 6; ++i)
  102. if (lph->bar[i])
  103. munmap(lph->bar[i], lph->barsz[i]);
  104. free(lph);
  105. }
  106. static
  107. const uint32_t *lowl_pci_get_words_mmap(struct lowl_pci_handle * const lph, void * const buf, const size_t words, const int bar, const off_t offset)
  108. {
  109. return &lph->bar[bar][offset];
  110. }
  111. static
  112. bool lowl_pci_set_words_mmap(struct lowl_pci_handle * const lph, const uint32_t *buf, const size_t words, const int bar, const off_t offset)
  113. {
  114. uint32_t *dest = &lph->bar[bar][offset];
  115. for (int i = 0; i < words; ++i)
  116. *(dest++) = *(buf++);
  117. return true;
  118. }
  119. static
  120. int _file_mode_to_mmap_prot(const int mode)
  121. {
  122. switch (mode)
  123. {
  124. case O_RDONLY:
  125. return PROT_READ;
  126. case O_WRONLY:
  127. return PROT_WRITE;
  128. case O_RDWR:
  129. return PROT_READ | PROT_WRITE;
  130. default:
  131. return -1;
  132. }
  133. }
  134. #endif
  135. #ifdef USE_LOWL_PCI_DATA_WRAPPERS
  136. static
  137. const void *lowl_pci_get_data_from_words(struct lowl_pci_handle * const lph, void * const bufp, const size_t sz, const int bar, const off_t offset)
  138. {
  139. uint8_t * const buf = bufp;
  140. const off_t offset32 = offset / 4;
  141. const off_t offset8 = offset % 4;
  142. const size_t words = (sz + offset8 + 3) / 4;
  143. const uint32_t * const wdata = lowl_pci_get_words(lph, buf, words, bar, offset32);
  144. swap32tobe(buf, wdata, words);
  145. return &buf[offset8];
  146. }
  147. static
  148. bool lowl_pci_set_data_in_words(struct lowl_pci_handle * const lph, const void * const bufp, size_t sz, const int bar, const off_t offset)
  149. {
  150. const uint8_t *buf = bufp;
  151. const off_t offset32 = offset / 4;
  152. off_t offset8 = offset % 4;
  153. const size_t words = (sz + offset8 + 3) / 4;
  154. uint32_t wdata[words], *wdp = wdata;
  155. if (offset8)
  156. {
  157. const uint32_t * const p = lowl_pci_get_words(lph, wdata, 1, bar, offset32);
  158. if (unlikely(!p))
  159. return false;
  160. wdata[0] = *p >> (32 - (8 * offset8));
  161. }
  162. for ( ; sz; --sz)
  163. {
  164. *wdp = (*wdp << 8) | *(buf++);
  165. if (++offset8 == 4)
  166. {
  167. offset8 = 0;
  168. ++wdp;
  169. }
  170. }
  171. if (offset8)
  172. {
  173. uint32_t u;
  174. const uint32_t * const p = lowl_pci_get_words(lph, &u, 1, bar, offset32 + words - 1);
  175. if (unlikely(!p))
  176. return false;
  177. const int n = 32 - (8 * offset8);
  178. wdp[0] <<= n;
  179. wdp[0] |= *p & ((1 << n) - 1);
  180. }
  181. return lowl_pci_set_words(lph, wdata, words, bar, offset32);
  182. }
  183. #endif
  184. #ifdef USE_UIO
  185. static const struct lowl_pci_interface lpi_uio;
  186. static
  187. void *_uio_mmap_bar(const char * const path, const int bar, const size_t sz, const int prot)
  188. {
  189. char buf[0x100];
  190. snprintf(buf, sizeof(buf), "/sys/bus/pci/devices/%s/resource%d", path, bar);
  191. const int fd = open(buf, O_RDWR);
  192. if (fd == -1)
  193. return MAP_FAILED;
  194. void * const rv = mmap(NULL, sz, prot, MAP_SHARED, fd, 0);
  195. close(fd);
  196. return rv;
  197. }
  198. struct lowl_pci_handle *lowl_pci_open_uio(const char * const path, const struct _lowl_pci_config * const barcfgs)
  199. {
  200. struct lowl_pci_handle * const lph = malloc(sizeof(*lph));
  201. *lph = (struct lowl_pci_handle){
  202. .path = path,
  203. .lpi = &lpi_uio,
  204. };
  205. for (const struct _lowl_pci_config *barcfg = barcfgs; barcfg->bar != -1; ++barcfg)
  206. {
  207. const int barno = barcfg->bar;
  208. const int prot = _file_mode_to_mmap_prot(barcfg->mode);
  209. if (unlikely(prot == -1))
  210. goto err;
  211. lph->bar[barno] = _uio_mmap_bar(path, barno, barcfg->sz, prot);
  212. lph->barsz[barno] = barcfg->sz;
  213. if (lph->bar[barno] == MAP_FAILED)
  214. {
  215. applog(LOG_ERR, "mmap %s bar %d failed", path, barno);
  216. goto err;
  217. }
  218. }
  219. return lph;
  220. err:
  221. for (int i = 0; i < 6; ++i)
  222. if (lph->bar[i])
  223. munmap(lph->bar[i], lph->barsz[i]);
  224. free(lph);
  225. return NULL;
  226. }
  227. static const struct lowl_pci_interface lpi_uio = {
  228. .open = lowl_pci_open_uio,
  229. .close = lowl_pci_close_mmap,
  230. .get_words = lowl_pci_get_words_mmap,
  231. .get_data = lowl_pci_get_data_from_words,
  232. .set_words = lowl_pci_set_words_mmap,
  233. .set_data = lowl_pci_set_data_in_words,
  234. };
  235. #endif
  236. #ifdef USE_VFIO
  237. static const struct lowl_pci_interface lpi_vfio;
  238. #define _VFIO_ACCESS_BAR_PROBLEM ((void*)&lpi_vfio)
  239. static
  240. void *_vfio_access_bar(const int device, const int bar, const size_t sz, const int prot, off_t *out_offset)
  241. {
  242. struct vfio_region_info region_info = { .argsz = sizeof(region_info) };
  243. switch (bar)
  244. {
  245. #define _BARCASE(n) \
  246. case n: \
  247. region_info.index = VFIO_PCI_BAR ## n ## _REGION_INDEX; \
  248. break;
  249. _BARCASE(0) _BARCASE(1) _BARCASE(2) _BARCASE(3)
  250. _BARCASE(4) _BARCASE(5)
  251. #undef _BARCASE
  252. default:
  253. return _VFIO_ACCESS_BAR_PROBLEM;
  254. }
  255. if (ioctl(device, VFIO_DEVICE_GET_REGION_INFO, &region_info))
  256. applogr(_VFIO_ACCESS_BAR_PROBLEM, LOG_ERR, "%s: VFIO_DEVICE_GET_REGION_INFO failed", __func__);
  257. if ((prot & PROT_READ ) && !(region_info.flags & VFIO_REGION_INFO_FLAG_READ ))
  258. applogr(_VFIO_ACCESS_BAR_PROBLEM, LOG_ERR, "%s: region does not support %s", __func__, "read");
  259. if ((prot & PROT_WRITE) && !(region_info.flags & VFIO_REGION_INFO_FLAG_WRITE))
  260. applogr(_VFIO_ACCESS_BAR_PROBLEM, LOG_ERR, "%s: region does not support %s", __func__, "write");
  261. if (region_info.size < sz)
  262. applogr(_VFIO_ACCESS_BAR_PROBLEM, LOG_ERR, "%s: region is only %lu bytes (needed %lu)",
  263. __func__, (unsigned long)region_info.size, (unsigned long)sz);
  264. *out_offset = region_info.offset;
  265. if (!(region_info.flags & VFIO_REGION_INFO_FLAG_MMAP))
  266. return MAP_FAILED;
  267. return mmap(NULL, sz, prot, MAP_SHARED, device, region_info.offset);
  268. }
  269. struct lowl_pci_handle *lowl_pci_open_vfio(const char * const path, const struct _lowl_pci_config * const barcfgs)
  270. {
  271. char buf[0x100], buf2[0x100];
  272. ssize_t ss;
  273. char *p;
  274. int group = -1, device = -1;
  275. off_t offset;
  276. struct lowl_pci_handle * const lph = malloc(sizeof(*lph));
  277. *lph = (struct lowl_pci_handle){
  278. .path = path,
  279. .lpi = &lpi_vfio,
  280. };
  281. const char * const vfio_path = "/dev/vfio/vfio";
  282. const int container = open(vfio_path, O_RDWR);
  283. if (container == -1)
  284. {
  285. applog(LOG_ERR, "%s: Failed to open %s", __func__, vfio_path);
  286. goto err;
  287. }
  288. {
  289. const int vfio_ver = ioctl(container, VFIO_GET_API_VERSION);
  290. if (vfio_ver != VFIO_API_VERSION)
  291. {
  292. applog(LOG_ERR, "%s: vfio API version mismatch (have=%d expect=%d)",
  293. __func__, vfio_ver, VFIO_API_VERSION);
  294. goto err;
  295. }
  296. }
  297. snprintf(buf, sizeof(buf), "/sys/bus/pci/devices/%s/iommu_group", path);
  298. ss = readlink(buf, buf2, sizeof(buf2) - 1);
  299. if (ss == -1)
  300. {
  301. applog(LOG_ERR, "%s: Failed to read %s", __func__, buf);
  302. goto err;
  303. }
  304. buf2[ss] = '\0';
  305. p = memrchr(buf2, '/', ss - 1);
  306. if (p)
  307. ++p;
  308. else
  309. p = buf2;
  310. snprintf(buf, sizeof(buf), "/dev/vfio/%s", p);
  311. group = open(buf, O_RDWR);
  312. if (group == -1)
  313. {
  314. applog(LOG_ERR, "%s: Failed to open %s", __func__, buf);
  315. goto err;
  316. }
  317. struct vfio_group_status group_status = { .argsz = sizeof(group_status) };
  318. if (ioctl(group, VFIO_GROUP_GET_STATUS, &group_status))
  319. {
  320. applog(LOG_ERR, "%s: VFIO_GROUP_GET_STATUS failed on iommu group %s", __func__, p);
  321. goto err;
  322. }
  323. if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
  324. {
  325. applog(LOG_ERR, "%s: iommu group %s is not viable", __func__, p);
  326. goto err;
  327. }
  328. if (ioctl(group, VFIO_GROUP_SET_CONTAINER, &container))
  329. {
  330. applog(LOG_ERR, "%s: VFIO_GROUP_SET_CONTAINER failed on iommu group %s", __func__, p);
  331. goto err;
  332. }
  333. if (ioctl(container, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU))
  334. {
  335. applog(LOG_ERR, "%s: Failed to set type1 iommu on group %s", __func__, p);
  336. goto err;
  337. }
  338. device = ioctl(group, VFIO_GROUP_GET_DEVICE_FD, path);
  339. if (device == -1)
  340. {
  341. applog(LOG_ERR, "%s: Failed to get device fd for %s in group %s", __func__, path, p);
  342. goto err;
  343. }
  344. for (const struct _lowl_pci_config *barcfg = barcfgs; barcfg->bar != -1; ++barcfg)
  345. {
  346. const int barno = barcfg->bar;
  347. const int prot = _file_mode_to_mmap_prot(barcfg->mode);
  348. if (unlikely(prot == -1))
  349. goto err;
  350. lph->bar[barno] = _vfio_access_bar(device, barno, barcfg->sz, prot, &offset);
  351. lph->barsz[barno] = barcfg->sz;
  352. if (lph->bar[barno] == _VFIO_ACCESS_BAR_PROBLEM)
  353. {
  354. applog(LOG_ERR, "%s: Accessing %s bar %d failed", __func__, path, barno);
  355. goto err;
  356. }
  357. else
  358. if (lph->bar[barno] == MAP_FAILED)
  359. lph->bar[barno] = NULL;
  360. lph->baroff[barno] = offset;
  361. }
  362. lph->fd[0] = device;
  363. lph->fd[1] = group;
  364. lph->fd[2] = container;
  365. return lph;
  366. err:
  367. for (int i = 0; i < 6; ++i)
  368. if (lph->bar[i])
  369. munmap(lph->bar[i], lph->barsz[i]);
  370. if (device != -1)
  371. close(device);
  372. if (group != -1)
  373. close(group);
  374. if (container != -1)
  375. close(container);
  376. free(lph);
  377. return NULL;
  378. }
  379. static
  380. void lowl_pci_close_vfio(struct lowl_pci_handle * const lph)
  381. {
  382. close(lph->fd[0]);
  383. close(lph->fd[1]);
  384. close(lph->fd[2]);
  385. lowl_pci_close_mmap(lph);
  386. }
  387. static
  388. const uint32_t *lowl_pci_get_words_vfio(struct lowl_pci_handle * const lph, void * const buf, const size_t words, const int bar, const off_t offset)
  389. {
  390. if (lph->bar[bar])
  391. return lowl_pci_get_words_mmap(lph, buf, words, bar, offset);
  392. const size_t sz = 4 * words;
  393. if (unlikely(sz != pread(lph->fd[0], buf, sz, (4 * offset) + lph->baroff[bar])))
  394. return NULL;
  395. return buf;
  396. }
  397. static
  398. bool lowl_pci_set_words_vfio(struct lowl_pci_handle * const lph, const uint32_t *buf, const size_t words, const int bar, const off_t offset)
  399. {
  400. if (lph->bar[bar])
  401. return lowl_pci_set_words_mmap(lph, buf, words, bar, offset);
  402. const size_t sz = 4 * words;
  403. if (unlikely(sz != pwrite(lph->fd[0], buf, sz, (4 * offset) + lph->baroff[bar])))
  404. return false;
  405. return true;
  406. }
  407. static const struct lowl_pci_interface lpi_vfio = {
  408. .open = lowl_pci_open_vfio,
  409. .close = lowl_pci_close_vfio,
  410. .get_words = lowl_pci_get_words_vfio,
  411. .get_data = lowl_pci_get_data_from_words,
  412. .set_words = lowl_pci_set_words_vfio,
  413. .set_data = lowl_pci_set_data_in_words,
  414. };
  415. #endif
  416. struct lowl_pci_handle *lowl_pci_open(const char * const path, const struct _lowl_pci_config * const barcfgs)
  417. {
  418. return
  419. #ifdef USE_VFIO
  420. lpi_vfio.open(path, barcfgs) ?:
  421. #endif
  422. #ifdef USE_UIO
  423. lpi_uio.open(path, barcfgs) ?:
  424. #endif
  425. false;
  426. }
  427. const uint32_t *lowl_pci_get_words(struct lowl_pci_handle * const lph, void * const buf, const size_t words, const int bar, const off_t offset)
  428. {
  429. return lph->lpi->get_words(lph, buf, words, bar, offset);
  430. }
  431. const void *lowl_pci_get_data(struct lowl_pci_handle * const lph, void * const buf, const size_t sz, const int bar, const off_t offset)
  432. {
  433. return lph->lpi->get_data(lph, buf, sz, bar, offset);
  434. }
  435. bool lowl_pci_set_words(struct lowl_pci_handle * const lph, const uint32_t * const buf, const size_t words, const int bar, const off_t offset)
  436. {
  437. return lph->lpi->set_words(lph, buf, words, bar, offset);
  438. }
  439. bool lowl_pci_set_data(struct lowl_pci_handle * const lph, const void * const buf, const size_t sz, const int bar, const off_t offset)
  440. {
  441. return lph->lpi->set_data(lph, buf, sz, bar, offset);
  442. }
  443. void lowl_pci_close(struct lowl_pci_handle * const lph)
  444. {
  445. return lph->lpi->close(lph);
  446. }
  447. struct lowlevel_driver lowl_pci = {
  448. .dname = "pci",
  449. .devinfo_scan = pci_devinfo_scan,
  450. .exclude_from_all = true,
  451. };