lowl-pci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. volatile 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((void*)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. volatile uint32_t *src = &lph->bar[bar][offset];
  110. uint32_t *dest = buf;
  111. for (int i = 0; i < words; ++i)
  112. *(dest++) = *(src++);
  113. return buf;
  114. }
  115. static
  116. 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)
  117. {
  118. volatile uint32_t *dest = &lph->bar[bar][offset];
  119. for (int i = 0; i < words; ++i)
  120. *(dest++) = *(buf++);
  121. return true;
  122. }
  123. static
  124. int _file_mode_to_mmap_prot(const int mode)
  125. {
  126. switch (mode)
  127. {
  128. case O_RDONLY:
  129. return PROT_READ;
  130. case O_WRONLY:
  131. return PROT_WRITE;
  132. case O_RDWR:
  133. return PROT_READ | PROT_WRITE;
  134. default:
  135. return -1;
  136. }
  137. }
  138. #endif
  139. #ifdef USE_LOWL_PCI_DATA_WRAPPERS
  140. static
  141. 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)
  142. {
  143. uint8_t * const buf = bufp;
  144. const off_t offset32 = offset / 4;
  145. const off_t offset8 = offset % 4;
  146. const size_t words = (sz + offset8 + 3) / 4;
  147. const uint32_t * const wdata = lowl_pci_get_words(lph, buf, words, bar, offset32);
  148. swap32tobe(buf, wdata, words);
  149. return &buf[offset8];
  150. }
  151. static
  152. 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)
  153. {
  154. const uint8_t *buf = bufp;
  155. const off_t offset32 = offset / 4;
  156. off_t offset8 = offset % 4;
  157. const size_t words = (sz + offset8 + 3) / 4;
  158. uint32_t wdata[words], *wdp = wdata;
  159. if (offset8)
  160. {
  161. const uint32_t * const p = lowl_pci_get_words(lph, wdata, 1, bar, offset32);
  162. if (unlikely(!p))
  163. return false;
  164. wdata[0] = *p >> (32 - (8 * offset8));
  165. }
  166. for ( ; sz; --sz)
  167. {
  168. *wdp = (*wdp << 8) | *(buf++);
  169. if (++offset8 == 4)
  170. {
  171. offset8 = 0;
  172. ++wdp;
  173. }
  174. }
  175. if (offset8)
  176. {
  177. uint32_t u;
  178. const uint32_t * const p = lowl_pci_get_words(lph, &u, 1, bar, offset32 + words - 1);
  179. if (unlikely(!p))
  180. return false;
  181. const int n = 32 - (8 * offset8);
  182. wdp[0] <<= n;
  183. wdp[0] |= *p & ((1 << n) - 1);
  184. }
  185. return lowl_pci_set_words(lph, wdata, words, bar, offset32);
  186. }
  187. #endif
  188. #ifdef USE_UIO
  189. static const struct lowl_pci_interface lpi_uio;
  190. static
  191. void *_uio_mmap_bar(const char * const path, const int bar, const size_t sz, const int prot)
  192. {
  193. char buf[0x100];
  194. snprintf(buf, sizeof(buf), "/sys/bus/pci/devices/%s/resource%d", path, bar);
  195. const int fd = open(buf, O_RDWR);
  196. if (fd == -1)
  197. return MAP_FAILED;
  198. void * const rv = mmap(NULL, sz, prot, MAP_SHARED, fd, 0);
  199. close(fd);
  200. return rv;
  201. }
  202. struct lowl_pci_handle *lowl_pci_open_uio(const char * const path, const struct _lowl_pci_config * const barcfgs)
  203. {
  204. struct lowl_pci_handle * const lph = malloc(sizeof(*lph));
  205. *lph = (struct lowl_pci_handle){
  206. .path = path,
  207. .lpi = &lpi_uio,
  208. };
  209. for (const struct _lowl_pci_config *barcfg = barcfgs; barcfg->bar != -1; ++barcfg)
  210. {
  211. const int barno = barcfg->bar;
  212. const int prot = _file_mode_to_mmap_prot(barcfg->mode);
  213. if (unlikely(prot == -1))
  214. goto err;
  215. lph->bar[barno] = _uio_mmap_bar(path, barno, barcfg->sz, prot);
  216. lph->barsz[barno] = barcfg->sz;
  217. if (lph->bar[barno] == MAP_FAILED)
  218. {
  219. applog(LOG_ERR, "mmap %s bar %d failed", path, barno);
  220. goto err;
  221. }
  222. }
  223. return lph;
  224. err:
  225. for (int i = 0; i < 6; ++i)
  226. if (lph->bar[i])
  227. munmap((void*)lph->bar[i], lph->barsz[i]);
  228. free(lph);
  229. return NULL;
  230. }
  231. static const struct lowl_pci_interface lpi_uio = {
  232. .open = lowl_pci_open_uio,
  233. .close = lowl_pci_close_mmap,
  234. .get_words = lowl_pci_get_words_mmap,
  235. .get_data = lowl_pci_get_data_from_words,
  236. .set_words = lowl_pci_set_words_mmap,
  237. .set_data = lowl_pci_set_data_in_words,
  238. };
  239. #endif
  240. #ifdef USE_VFIO
  241. static const struct lowl_pci_interface lpi_vfio;
  242. #define _VFIO_ACCESS_BAR_PROBLEM ((void*)&lpi_vfio)
  243. static
  244. void *_vfio_access_bar(const int device, const int bar, const size_t sz, const int prot, off_t *out_offset)
  245. {
  246. struct vfio_region_info region_info = { .argsz = sizeof(region_info) };
  247. switch (bar)
  248. {
  249. #define _BARCASE(n) \
  250. case n: \
  251. region_info.index = VFIO_PCI_BAR ## n ## _REGION_INDEX; \
  252. break;
  253. _BARCASE(0) _BARCASE(1) _BARCASE(2) _BARCASE(3)
  254. _BARCASE(4) _BARCASE(5)
  255. #undef _BARCASE
  256. default:
  257. return _VFIO_ACCESS_BAR_PROBLEM;
  258. }
  259. if (ioctl(device, VFIO_DEVICE_GET_REGION_INFO, &region_info))
  260. applogr(_VFIO_ACCESS_BAR_PROBLEM, LOG_ERR, "%s: VFIO_DEVICE_GET_REGION_INFO failed", __func__);
  261. if ((prot & PROT_READ ) && !(region_info.flags & VFIO_REGION_INFO_FLAG_READ ))
  262. applogr(_VFIO_ACCESS_BAR_PROBLEM, LOG_ERR, "%s: region does not support %s", __func__, "read");
  263. if ((prot & PROT_WRITE) && !(region_info.flags & VFIO_REGION_INFO_FLAG_WRITE))
  264. applogr(_VFIO_ACCESS_BAR_PROBLEM, LOG_ERR, "%s: region does not support %s", __func__, "write");
  265. if (region_info.size < sz)
  266. applogr(_VFIO_ACCESS_BAR_PROBLEM, LOG_ERR, "%s: region is only %lu bytes (needed %lu)",
  267. __func__, (unsigned long)region_info.size, (unsigned long)sz);
  268. *out_offset = region_info.offset;
  269. if (!(region_info.flags & VFIO_REGION_INFO_FLAG_MMAP))
  270. return MAP_FAILED;
  271. return mmap(NULL, sz, prot, MAP_SHARED, device, region_info.offset);
  272. }
  273. struct lowl_pci_handle *lowl_pci_open_vfio(const char * const path, const struct _lowl_pci_config * const barcfgs)
  274. {
  275. char buf[0x100], buf2[0x100];
  276. ssize_t ss;
  277. char *p;
  278. int group = -1, device = -1;
  279. off_t offset;
  280. struct lowl_pci_handle * const lph = malloc(sizeof(*lph));
  281. *lph = (struct lowl_pci_handle){
  282. .path = path,
  283. .lpi = &lpi_vfio,
  284. };
  285. const char * const vfio_path = "/dev/vfio/vfio";
  286. const int container = open(vfio_path, O_RDWR);
  287. if (container == -1)
  288. {
  289. applog(LOG_ERR, "%s: Failed to open %s", __func__, vfio_path);
  290. goto err;
  291. }
  292. {
  293. const int vfio_ver = ioctl(container, VFIO_GET_API_VERSION);
  294. if (vfio_ver != VFIO_API_VERSION)
  295. {
  296. applog(LOG_ERR, "%s: vfio API version mismatch (have=%d expect=%d)",
  297. __func__, vfio_ver, VFIO_API_VERSION);
  298. goto err;
  299. }
  300. }
  301. snprintf(buf, sizeof(buf), "/sys/bus/pci/devices/%s/iommu_group", path);
  302. ss = readlink(buf, buf2, sizeof(buf2) - 1);
  303. if (ss == -1)
  304. {
  305. applog(LOG_ERR, "%s: Failed to read %s", __func__, buf);
  306. goto err;
  307. }
  308. buf2[ss] = '\0';
  309. p = memrchr(buf2, '/', ss - 1);
  310. if (p)
  311. ++p;
  312. else
  313. p = buf2;
  314. snprintf(buf, sizeof(buf), "/dev/vfio/%s", p);
  315. group = open(buf, O_RDWR);
  316. if (group == -1)
  317. {
  318. applog(LOG_ERR, "%s: Failed to open %s", __func__, buf);
  319. goto err;
  320. }
  321. struct vfio_group_status group_status = { .argsz = sizeof(group_status) };
  322. if (ioctl(group, VFIO_GROUP_GET_STATUS, &group_status))
  323. {
  324. applog(LOG_ERR, "%s: VFIO_GROUP_GET_STATUS failed on iommu group %s", __func__, p);
  325. goto err;
  326. }
  327. if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
  328. {
  329. applog(LOG_ERR, "%s: iommu group %s is not viable", __func__, p);
  330. goto err;
  331. }
  332. if (ioctl(group, VFIO_GROUP_SET_CONTAINER, &container))
  333. {
  334. applog(LOG_ERR, "%s: VFIO_GROUP_SET_CONTAINER failed on iommu group %s", __func__, p);
  335. goto err;
  336. }
  337. if (ioctl(container, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU))
  338. {
  339. applog(LOG_ERR, "%s: Failed to set type1 iommu on group %s", __func__, p);
  340. goto err;
  341. }
  342. device = ioctl(group, VFIO_GROUP_GET_DEVICE_FD, path);
  343. if (device == -1)
  344. {
  345. applog(LOG_ERR, "%s: Failed to get device fd for %s in group %s", __func__, path, p);
  346. goto err;
  347. }
  348. for (const struct _lowl_pci_config *barcfg = barcfgs; barcfg->bar != -1; ++barcfg)
  349. {
  350. const int barno = barcfg->bar;
  351. const int prot = _file_mode_to_mmap_prot(barcfg->mode);
  352. if (unlikely(prot == -1))
  353. goto err;
  354. lph->bar[barno] = _vfio_access_bar(device, barno, barcfg->sz, prot, &offset);
  355. lph->barsz[barno] = barcfg->sz;
  356. if (lph->bar[barno] == _VFIO_ACCESS_BAR_PROBLEM)
  357. {
  358. applog(LOG_ERR, "%s: Accessing %s bar %d failed", __func__, path, barno);
  359. goto err;
  360. }
  361. else
  362. if (lph->bar[barno] == MAP_FAILED)
  363. lph->bar[barno] = NULL;
  364. lph->baroff[barno] = offset;
  365. }
  366. lph->fd[0] = device;
  367. lph->fd[1] = group;
  368. lph->fd[2] = container;
  369. return lph;
  370. err:
  371. for (int i = 0; i < 6; ++i)
  372. if (lph->bar[i])
  373. munmap((void*)lph->bar[i], lph->barsz[i]);
  374. if (device != -1)
  375. close(device);
  376. if (group != -1)
  377. close(group);
  378. if (container != -1)
  379. close(container);
  380. free(lph);
  381. return NULL;
  382. }
  383. static
  384. void lowl_pci_close_vfio(struct lowl_pci_handle * const lph)
  385. {
  386. close(lph->fd[0]);
  387. close(lph->fd[1]);
  388. close(lph->fd[2]);
  389. lowl_pci_close_mmap(lph);
  390. }
  391. static
  392. 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)
  393. {
  394. if (lph->bar[bar])
  395. return lowl_pci_get_words_mmap(lph, buf, words, bar, offset);
  396. const size_t sz = 4 * words;
  397. if (unlikely(sz != pread(lph->fd[0], buf, sz, (4 * offset) + lph->baroff[bar])))
  398. return NULL;
  399. return buf;
  400. }
  401. static
  402. 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)
  403. {
  404. if (lph->bar[bar])
  405. return lowl_pci_set_words_mmap(lph, buf, words, bar, offset);
  406. const size_t sz = 4 * words;
  407. if (unlikely(sz != pwrite(lph->fd[0], buf, sz, (4 * offset) + lph->baroff[bar])))
  408. return false;
  409. return true;
  410. }
  411. static const struct lowl_pci_interface lpi_vfio = {
  412. .open = lowl_pci_open_vfio,
  413. .close = lowl_pci_close_vfio,
  414. .get_words = lowl_pci_get_words_vfio,
  415. .get_data = lowl_pci_get_data_from_words,
  416. .set_words = lowl_pci_set_words_vfio,
  417. .set_data = lowl_pci_set_data_in_words,
  418. };
  419. #endif
  420. struct lowl_pci_handle *lowl_pci_open(const char * const path, const struct _lowl_pci_config * const barcfgs)
  421. {
  422. return
  423. #ifdef USE_VFIO
  424. lpi_vfio.open(path, barcfgs) ?:
  425. #endif
  426. #ifdef USE_UIO
  427. lpi_uio.open(path, barcfgs) ?:
  428. #endif
  429. false;
  430. }
  431. 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)
  432. {
  433. return lph->lpi->get_words(lph, buf, words, bar, offset);
  434. }
  435. 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)
  436. {
  437. return lph->lpi->get_data(lph, buf, sz, bar, offset);
  438. }
  439. 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)
  440. {
  441. return lph->lpi->set_words(lph, buf, words, bar, offset);
  442. }
  443. 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)
  444. {
  445. return lph->lpi->set_data(lph, buf, sz, bar, offset);
  446. }
  447. void lowl_pci_close(struct lowl_pci_handle * const lph)
  448. {
  449. return lph->lpi->close(lph);
  450. }
  451. struct lowlevel_driver lowl_pci = {
  452. .dname = "pci",
  453. .devinfo_scan = pci_devinfo_scan,
  454. .exclude_from_all = true,
  455. };