virtio-iommu.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Virtio driver for the paravirtualized IOMMU
  4. *
  5. * Copyright (C) 2019 Arm Limited
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/amba/bus.h>
  9. #include <linux/delay.h>
  10. #include <linux/dma-iommu.h>
  11. #include <linux/freezer.h>
  12. #include <linux/interval_tree.h>
  13. #include <linux/iommu.h>
  14. #include <linux/module.h>
  15. #include <linux/of_iommu.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/pci.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/virtio.h>
  20. #include <linux/virtio_config.h>
  21. #include <linux/virtio_ids.h>
  22. #include <linux/wait.h>
  23. #include <uapi/linux/virtio_iommu.h>
  24. #define MSI_IOVA_BASE 0x8000000
  25. #define MSI_IOVA_LENGTH 0x100000
  26. #define VIOMMU_REQUEST_VQ 0
  27. #define VIOMMU_EVENT_VQ 1
  28. #define VIOMMU_NR_VQS 2
  29. struct viommu_dev {
  30. struct iommu_device iommu;
  31. struct device *dev;
  32. struct virtio_device *vdev;
  33. struct ida domain_ids;
  34. struct virtqueue *vqs[VIOMMU_NR_VQS];
  35. spinlock_t request_lock;
  36. struct list_head requests;
  37. void *evts;
  38. /* Device configuration */
  39. struct iommu_domain_geometry geometry;
  40. u64 pgsize_bitmap;
  41. u32 first_domain;
  42. u32 last_domain;
  43. /* Supported MAP flags */
  44. u32 map_flags;
  45. u32 probe_size;
  46. };
  47. struct viommu_mapping {
  48. phys_addr_t paddr;
  49. struct interval_tree_node iova;
  50. u32 flags;
  51. };
  52. struct viommu_domain {
  53. struct iommu_domain domain;
  54. struct viommu_dev *viommu;
  55. struct mutex mutex; /* protects viommu pointer */
  56. unsigned int id;
  57. u32 map_flags;
  58. spinlock_t mappings_lock;
  59. struct rb_root_cached mappings;
  60. unsigned long nr_endpoints;
  61. };
  62. struct viommu_endpoint {
  63. struct device *dev;
  64. struct viommu_dev *viommu;
  65. struct viommu_domain *vdomain;
  66. struct list_head resv_regions;
  67. };
  68. struct viommu_request {
  69. struct list_head list;
  70. void *writeback;
  71. unsigned int write_offset;
  72. unsigned int len;
  73. char buf[];
  74. };
  75. #define VIOMMU_FAULT_RESV_MASK 0xffffff00
  76. struct viommu_event {
  77. union {
  78. u32 head;
  79. struct virtio_iommu_fault fault;
  80. };
  81. };
  82. #define to_viommu_domain(domain) \
  83. container_of(domain, struct viommu_domain, domain)
  84. static int viommu_get_req_errno(void *buf, size_t len)
  85. {
  86. struct virtio_iommu_req_tail *tail = buf + len - sizeof(*tail);
  87. switch (tail->status) {
  88. case VIRTIO_IOMMU_S_OK:
  89. return 0;
  90. case VIRTIO_IOMMU_S_UNSUPP:
  91. return -ENOSYS;
  92. case VIRTIO_IOMMU_S_INVAL:
  93. return -EINVAL;
  94. case VIRTIO_IOMMU_S_RANGE:
  95. return -ERANGE;
  96. case VIRTIO_IOMMU_S_NOENT:
  97. return -ENOENT;
  98. case VIRTIO_IOMMU_S_FAULT:
  99. return -EFAULT;
  100. case VIRTIO_IOMMU_S_NOMEM:
  101. return -ENOMEM;
  102. case VIRTIO_IOMMU_S_IOERR:
  103. case VIRTIO_IOMMU_S_DEVERR:
  104. default:
  105. return -EIO;
  106. }
  107. }
  108. static void viommu_set_req_status(void *buf, size_t len, int status)
  109. {
  110. struct virtio_iommu_req_tail *tail = buf + len - sizeof(*tail);
  111. tail->status = status;
  112. }
  113. static off_t viommu_get_write_desc_offset(struct viommu_dev *viommu,
  114. struct virtio_iommu_req_head *req,
  115. size_t len)
  116. {
  117. size_t tail_size = sizeof(struct virtio_iommu_req_tail);
  118. if (req->type == VIRTIO_IOMMU_T_PROBE)
  119. return len - viommu->probe_size - tail_size;
  120. return len - tail_size;
  121. }
  122. /*
  123. * __viommu_sync_req - Complete all in-flight requests
  124. *
  125. * Wait for all added requests to complete. When this function returns, all
  126. * requests that were in-flight at the time of the call have completed.
  127. */
  128. static int __viommu_sync_req(struct viommu_dev *viommu)
  129. {
  130. unsigned int len;
  131. size_t write_len;
  132. struct viommu_request *req;
  133. struct virtqueue *vq = viommu->vqs[VIOMMU_REQUEST_VQ];
  134. assert_spin_locked(&viommu->request_lock);
  135. virtqueue_kick(vq);
  136. while (!list_empty(&viommu->requests)) {
  137. len = 0;
  138. req = virtqueue_get_buf(vq, &len);
  139. if (!req)
  140. continue;
  141. if (!len)
  142. viommu_set_req_status(req->buf, req->len,
  143. VIRTIO_IOMMU_S_IOERR);
  144. write_len = req->len - req->write_offset;
  145. if (req->writeback && len == write_len)
  146. memcpy(req->writeback, req->buf + req->write_offset,
  147. write_len);
  148. list_del(&req->list);
  149. kfree(req);
  150. }
  151. return 0;
  152. }
  153. static int viommu_sync_req(struct viommu_dev *viommu)
  154. {
  155. int ret;
  156. unsigned long flags;
  157. spin_lock_irqsave(&viommu->request_lock, flags);
  158. ret = __viommu_sync_req(viommu);
  159. if (ret)
  160. dev_dbg(viommu->dev, "could not sync requests (%d)\n", ret);
  161. spin_unlock_irqrestore(&viommu->request_lock, flags);
  162. return ret;
  163. }
  164. /*
  165. * __viommu_add_request - Add one request to the queue
  166. * @buf: pointer to the request buffer
  167. * @len: length of the request buffer
  168. * @writeback: copy data back to the buffer when the request completes.
  169. *
  170. * Add a request to the queue. Only synchronize the queue if it's already full.
  171. * Otherwise don't kick the queue nor wait for requests to complete.
  172. *
  173. * When @writeback is true, data written by the device, including the request
  174. * status, is copied into @buf after the request completes. This is unsafe if
  175. * the caller allocates @buf on stack and drops the lock between add_req() and
  176. * sync_req().
  177. *
  178. * Return 0 if the request was successfully added to the queue.
  179. */
  180. static int __viommu_add_req(struct viommu_dev *viommu, void *buf, size_t len,
  181. bool writeback)
  182. {
  183. int ret;
  184. off_t write_offset;
  185. struct viommu_request *req;
  186. struct scatterlist top_sg, bottom_sg;
  187. struct scatterlist *sg[2] = { &top_sg, &bottom_sg };
  188. struct virtqueue *vq = viommu->vqs[VIOMMU_REQUEST_VQ];
  189. assert_spin_locked(&viommu->request_lock);
  190. write_offset = viommu_get_write_desc_offset(viommu, buf, len);
  191. if (write_offset <= 0)
  192. return -EINVAL;
  193. req = kzalloc(sizeof(*req) + len, GFP_ATOMIC);
  194. if (!req)
  195. return -ENOMEM;
  196. req->len = len;
  197. if (writeback) {
  198. req->writeback = buf + write_offset;
  199. req->write_offset = write_offset;
  200. }
  201. memcpy(&req->buf, buf, write_offset);
  202. sg_init_one(&top_sg, req->buf, write_offset);
  203. sg_init_one(&bottom_sg, req->buf + write_offset, len - write_offset);
  204. ret = virtqueue_add_sgs(vq, sg, 1, 1, req, GFP_ATOMIC);
  205. if (ret == -ENOSPC) {
  206. /* If the queue is full, sync and retry */
  207. if (!__viommu_sync_req(viommu))
  208. ret = virtqueue_add_sgs(vq, sg, 1, 1, req, GFP_ATOMIC);
  209. }
  210. if (ret)
  211. goto err_free;
  212. list_add_tail(&req->list, &viommu->requests);
  213. return 0;
  214. err_free:
  215. kfree(req);
  216. return ret;
  217. }
  218. static int viommu_add_req(struct viommu_dev *viommu, void *buf, size_t len)
  219. {
  220. int ret;
  221. unsigned long flags;
  222. spin_lock_irqsave(&viommu->request_lock, flags);
  223. ret = __viommu_add_req(viommu, buf, len, false);
  224. if (ret)
  225. dev_dbg(viommu->dev, "could not add request: %d\n", ret);
  226. spin_unlock_irqrestore(&viommu->request_lock, flags);
  227. return ret;
  228. }
  229. /*
  230. * Send a request and wait for it to complete. Return the request status (as an
  231. * errno)
  232. */
  233. static int viommu_send_req_sync(struct viommu_dev *viommu, void *buf,
  234. size_t len)
  235. {
  236. int ret;
  237. unsigned long flags;
  238. spin_lock_irqsave(&viommu->request_lock, flags);
  239. ret = __viommu_add_req(viommu, buf, len, true);
  240. if (ret) {
  241. dev_dbg(viommu->dev, "could not add request (%d)\n", ret);
  242. goto out_unlock;
  243. }
  244. ret = __viommu_sync_req(viommu);
  245. if (ret) {
  246. dev_dbg(viommu->dev, "could not sync requests (%d)\n", ret);
  247. /* Fall-through (get the actual request status) */
  248. }
  249. ret = viommu_get_req_errno(buf, len);
  250. out_unlock:
  251. spin_unlock_irqrestore(&viommu->request_lock, flags);
  252. return ret;
  253. }
  254. /*
  255. * viommu_add_mapping - add a mapping to the internal tree
  256. *
  257. * On success, return the new mapping. Otherwise return NULL.
  258. */
  259. static int viommu_add_mapping(struct viommu_domain *vdomain, unsigned long iova,
  260. phys_addr_t paddr, size_t size, u32 flags)
  261. {
  262. unsigned long irqflags;
  263. struct viommu_mapping *mapping;
  264. mapping = kzalloc(sizeof(*mapping), GFP_ATOMIC);
  265. if (!mapping)
  266. return -ENOMEM;
  267. mapping->paddr = paddr;
  268. mapping->iova.start = iova;
  269. mapping->iova.last = iova + size - 1;
  270. mapping->flags = flags;
  271. spin_lock_irqsave(&vdomain->mappings_lock, irqflags);
  272. interval_tree_insert(&mapping->iova, &vdomain->mappings);
  273. spin_unlock_irqrestore(&vdomain->mappings_lock, irqflags);
  274. return 0;
  275. }
  276. /*
  277. * viommu_del_mappings - remove mappings from the internal tree
  278. *
  279. * @vdomain: the domain
  280. * @iova: start of the range
  281. * @size: size of the range. A size of 0 corresponds to the entire address
  282. * space.
  283. *
  284. * On success, returns the number of unmapped bytes (>= size)
  285. */
  286. static size_t viommu_del_mappings(struct viommu_domain *vdomain,
  287. unsigned long iova, size_t size)
  288. {
  289. size_t unmapped = 0;
  290. unsigned long flags;
  291. unsigned long last = iova + size - 1;
  292. struct viommu_mapping *mapping = NULL;
  293. struct interval_tree_node *node, *next;
  294. spin_lock_irqsave(&vdomain->mappings_lock, flags);
  295. next = interval_tree_iter_first(&vdomain->mappings, iova, last);
  296. while (next) {
  297. node = next;
  298. mapping = container_of(node, struct viommu_mapping, iova);
  299. next = interval_tree_iter_next(node, iova, last);
  300. /* Trying to split a mapping? */
  301. if (mapping->iova.start < iova)
  302. break;
  303. /*
  304. * Virtio-iommu doesn't allow UNMAP to split a mapping created
  305. * with a single MAP request, so remove the full mapping.
  306. */
  307. unmapped += mapping->iova.last - mapping->iova.start + 1;
  308. interval_tree_remove(node, &vdomain->mappings);
  309. kfree(mapping);
  310. }
  311. spin_unlock_irqrestore(&vdomain->mappings_lock, flags);
  312. return unmapped;
  313. }
  314. /*
  315. * viommu_replay_mappings - re-send MAP requests
  316. *
  317. * When reattaching a domain that was previously detached from all endpoints,
  318. * mappings were deleted from the device. Re-create the mappings available in
  319. * the internal tree.
  320. */
  321. static int viommu_replay_mappings(struct viommu_domain *vdomain)
  322. {
  323. int ret = 0;
  324. unsigned long flags;
  325. struct viommu_mapping *mapping;
  326. struct interval_tree_node *node;
  327. struct virtio_iommu_req_map map;
  328. spin_lock_irqsave(&vdomain->mappings_lock, flags);
  329. node = interval_tree_iter_first(&vdomain->mappings, 0, -1UL);
  330. while (node) {
  331. mapping = container_of(node, struct viommu_mapping, iova);
  332. map = (struct virtio_iommu_req_map) {
  333. .head.type = VIRTIO_IOMMU_T_MAP,
  334. .domain = cpu_to_le32(vdomain->id),
  335. .virt_start = cpu_to_le64(mapping->iova.start),
  336. .virt_end = cpu_to_le64(mapping->iova.last),
  337. .phys_start = cpu_to_le64(mapping->paddr),
  338. .flags = cpu_to_le32(mapping->flags),
  339. };
  340. ret = viommu_send_req_sync(vdomain->viommu, &map, sizeof(map));
  341. if (ret)
  342. break;
  343. node = interval_tree_iter_next(node, 0, -1UL);
  344. }
  345. spin_unlock_irqrestore(&vdomain->mappings_lock, flags);
  346. return ret;
  347. }
  348. static int viommu_add_resv_mem(struct viommu_endpoint *vdev,
  349. struct virtio_iommu_probe_resv_mem *mem,
  350. size_t len)
  351. {
  352. size_t size;
  353. u64 start64, end64;
  354. phys_addr_t start, end;
  355. struct iommu_resv_region *region = NULL;
  356. unsigned long prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
  357. start = start64 = le64_to_cpu(mem->start);
  358. end = end64 = le64_to_cpu(mem->end);
  359. size = end64 - start64 + 1;
  360. /* Catch any overflow, including the unlikely end64 - start64 + 1 = 0 */
  361. if (start != start64 || end != end64 || size < end64 - start64)
  362. return -EOVERFLOW;
  363. if (len < sizeof(*mem))
  364. return -EINVAL;
  365. switch (mem->subtype) {
  366. default:
  367. dev_warn(vdev->dev, "unknown resv mem subtype 0x%x\n",
  368. mem->subtype);
  369. fallthrough;
  370. case VIRTIO_IOMMU_RESV_MEM_T_RESERVED:
  371. region = iommu_alloc_resv_region(start, size, 0,
  372. IOMMU_RESV_RESERVED);
  373. break;
  374. case VIRTIO_IOMMU_RESV_MEM_T_MSI:
  375. region = iommu_alloc_resv_region(start, size, prot,
  376. IOMMU_RESV_MSI);
  377. break;
  378. }
  379. if (!region)
  380. return -ENOMEM;
  381. list_add(&region->list, &vdev->resv_regions);
  382. return 0;
  383. }
  384. static int viommu_probe_endpoint(struct viommu_dev *viommu, struct device *dev)
  385. {
  386. int ret;
  387. u16 type, len;
  388. size_t cur = 0;
  389. size_t probe_len;
  390. struct virtio_iommu_req_probe *probe;
  391. struct virtio_iommu_probe_property *prop;
  392. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  393. struct viommu_endpoint *vdev = dev_iommu_priv_get(dev);
  394. if (!fwspec->num_ids)
  395. return -EINVAL;
  396. probe_len = sizeof(*probe) + viommu->probe_size +
  397. sizeof(struct virtio_iommu_req_tail);
  398. probe = kzalloc(probe_len, GFP_KERNEL);
  399. if (!probe)
  400. return -ENOMEM;
  401. probe->head.type = VIRTIO_IOMMU_T_PROBE;
  402. /*
  403. * For now, assume that properties of an endpoint that outputs multiple
  404. * IDs are consistent. Only probe the first one.
  405. */
  406. probe->endpoint = cpu_to_le32(fwspec->ids[0]);
  407. ret = viommu_send_req_sync(viommu, probe, probe_len);
  408. if (ret)
  409. goto out_free;
  410. prop = (void *)probe->properties;
  411. type = le16_to_cpu(prop->type) & VIRTIO_IOMMU_PROBE_T_MASK;
  412. while (type != VIRTIO_IOMMU_PROBE_T_NONE &&
  413. cur < viommu->probe_size) {
  414. len = le16_to_cpu(prop->length) + sizeof(*prop);
  415. switch (type) {
  416. case VIRTIO_IOMMU_PROBE_T_RESV_MEM:
  417. ret = viommu_add_resv_mem(vdev, (void *)prop, len);
  418. break;
  419. default:
  420. dev_err(dev, "unknown viommu prop 0x%x\n", type);
  421. }
  422. if (ret)
  423. dev_err(dev, "failed to parse viommu prop 0x%x\n", type);
  424. cur += len;
  425. if (cur >= viommu->probe_size)
  426. break;
  427. prop = (void *)probe->properties + cur;
  428. type = le16_to_cpu(prop->type) & VIRTIO_IOMMU_PROBE_T_MASK;
  429. }
  430. out_free:
  431. kfree(probe);
  432. return ret;
  433. }
  434. static int viommu_fault_handler(struct viommu_dev *viommu,
  435. struct virtio_iommu_fault *fault)
  436. {
  437. char *reason_str;
  438. u8 reason = fault->reason;
  439. u32 flags = le32_to_cpu(fault->flags);
  440. u32 endpoint = le32_to_cpu(fault->endpoint);
  441. u64 address = le64_to_cpu(fault->address);
  442. switch (reason) {
  443. case VIRTIO_IOMMU_FAULT_R_DOMAIN:
  444. reason_str = "domain";
  445. break;
  446. case VIRTIO_IOMMU_FAULT_R_MAPPING:
  447. reason_str = "page";
  448. break;
  449. case VIRTIO_IOMMU_FAULT_R_UNKNOWN:
  450. default:
  451. reason_str = "unknown";
  452. break;
  453. }
  454. /* TODO: find EP by ID and report_iommu_fault */
  455. if (flags & VIRTIO_IOMMU_FAULT_F_ADDRESS)
  456. dev_err_ratelimited(viommu->dev, "%s fault from EP %u at %#llx [%s%s%s]\n",
  457. reason_str, endpoint, address,
  458. flags & VIRTIO_IOMMU_FAULT_F_READ ? "R" : "",
  459. flags & VIRTIO_IOMMU_FAULT_F_WRITE ? "W" : "",
  460. flags & VIRTIO_IOMMU_FAULT_F_EXEC ? "X" : "");
  461. else
  462. dev_err_ratelimited(viommu->dev, "%s fault from EP %u\n",
  463. reason_str, endpoint);
  464. return 0;
  465. }
  466. static void viommu_event_handler(struct virtqueue *vq)
  467. {
  468. int ret;
  469. unsigned int len;
  470. struct scatterlist sg[1];
  471. struct viommu_event *evt;
  472. struct viommu_dev *viommu = vq->vdev->priv;
  473. while ((evt = virtqueue_get_buf(vq, &len)) != NULL) {
  474. if (len > sizeof(*evt)) {
  475. dev_err(viommu->dev,
  476. "invalid event buffer (len %u != %zu)\n",
  477. len, sizeof(*evt));
  478. } else if (!(evt->head & VIOMMU_FAULT_RESV_MASK)) {
  479. viommu_fault_handler(viommu, &evt->fault);
  480. }
  481. sg_init_one(sg, evt, sizeof(*evt));
  482. ret = virtqueue_add_inbuf(vq, sg, 1, evt, GFP_ATOMIC);
  483. if (ret)
  484. dev_err(viommu->dev, "could not add event buffer\n");
  485. }
  486. virtqueue_kick(vq);
  487. }
  488. /* IOMMU API */
  489. static struct iommu_domain *viommu_domain_alloc(unsigned type)
  490. {
  491. struct viommu_domain *vdomain;
  492. if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
  493. return NULL;
  494. vdomain = kzalloc(sizeof(*vdomain), GFP_KERNEL);
  495. if (!vdomain)
  496. return NULL;
  497. mutex_init(&vdomain->mutex);
  498. spin_lock_init(&vdomain->mappings_lock);
  499. vdomain->mappings = RB_ROOT_CACHED;
  500. if (type == IOMMU_DOMAIN_DMA &&
  501. iommu_get_dma_cookie(&vdomain->domain)) {
  502. kfree(vdomain);
  503. return NULL;
  504. }
  505. return &vdomain->domain;
  506. }
  507. static int viommu_domain_finalise(struct viommu_endpoint *vdev,
  508. struct iommu_domain *domain)
  509. {
  510. int ret;
  511. unsigned long viommu_page_size;
  512. struct viommu_dev *viommu = vdev->viommu;
  513. struct viommu_domain *vdomain = to_viommu_domain(domain);
  514. viommu_page_size = 1UL << __ffs(viommu->pgsize_bitmap);
  515. if (viommu_page_size > PAGE_SIZE) {
  516. dev_err(vdev->dev,
  517. "granule 0x%lx larger than system page size 0x%lx\n",
  518. viommu_page_size, PAGE_SIZE);
  519. return -EINVAL;
  520. }
  521. ret = ida_alloc_range(&viommu->domain_ids, viommu->first_domain,
  522. viommu->last_domain, GFP_KERNEL);
  523. if (ret < 0)
  524. return ret;
  525. vdomain->id = (unsigned int)ret;
  526. domain->pgsize_bitmap = viommu->pgsize_bitmap;
  527. domain->geometry = viommu->geometry;
  528. vdomain->map_flags = viommu->map_flags;
  529. vdomain->viommu = viommu;
  530. return 0;
  531. }
  532. static void viommu_domain_free(struct iommu_domain *domain)
  533. {
  534. struct viommu_domain *vdomain = to_viommu_domain(domain);
  535. iommu_put_dma_cookie(domain);
  536. /* Free all remaining mappings (size 2^64) */
  537. viommu_del_mappings(vdomain, 0, 0);
  538. if (vdomain->viommu)
  539. ida_free(&vdomain->viommu->domain_ids, vdomain->id);
  540. kfree(vdomain);
  541. }
  542. static int viommu_attach_dev(struct iommu_domain *domain, struct device *dev)
  543. {
  544. int i;
  545. int ret = 0;
  546. struct virtio_iommu_req_attach req;
  547. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  548. struct viommu_endpoint *vdev = dev_iommu_priv_get(dev);
  549. struct viommu_domain *vdomain = to_viommu_domain(domain);
  550. mutex_lock(&vdomain->mutex);
  551. if (!vdomain->viommu) {
  552. /*
  553. * Properly initialize the domain now that we know which viommu
  554. * owns it.
  555. */
  556. ret = viommu_domain_finalise(vdev, domain);
  557. } else if (vdomain->viommu != vdev->viommu) {
  558. dev_err(dev, "cannot attach to foreign vIOMMU\n");
  559. ret = -EXDEV;
  560. }
  561. mutex_unlock(&vdomain->mutex);
  562. if (ret)
  563. return ret;
  564. /*
  565. * In the virtio-iommu device, when attaching the endpoint to a new
  566. * domain, it is detached from the old one and, if as as a result the
  567. * old domain isn't attached to any endpoint, all mappings are removed
  568. * from the old domain and it is freed.
  569. *
  570. * In the driver the old domain still exists, and its mappings will be
  571. * recreated if it gets reattached to an endpoint. Otherwise it will be
  572. * freed explicitly.
  573. *
  574. * vdev->vdomain is protected by group->mutex
  575. */
  576. if (vdev->vdomain)
  577. vdev->vdomain->nr_endpoints--;
  578. req = (struct virtio_iommu_req_attach) {
  579. .head.type = VIRTIO_IOMMU_T_ATTACH,
  580. .domain = cpu_to_le32(vdomain->id),
  581. };
  582. for (i = 0; i < fwspec->num_ids; i++) {
  583. req.endpoint = cpu_to_le32(fwspec->ids[i]);
  584. ret = viommu_send_req_sync(vdomain->viommu, &req, sizeof(req));
  585. if (ret)
  586. return ret;
  587. }
  588. if (!vdomain->nr_endpoints) {
  589. /*
  590. * This endpoint is the first to be attached to the domain.
  591. * Replay existing mappings (e.g. SW MSI).
  592. */
  593. ret = viommu_replay_mappings(vdomain);
  594. if (ret)
  595. return ret;
  596. }
  597. vdomain->nr_endpoints++;
  598. vdev->vdomain = vdomain;
  599. return 0;
  600. }
  601. static int viommu_map(struct iommu_domain *domain, unsigned long iova,
  602. phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
  603. {
  604. int ret;
  605. u32 flags;
  606. struct virtio_iommu_req_map map;
  607. struct viommu_domain *vdomain = to_viommu_domain(domain);
  608. flags = (prot & IOMMU_READ ? VIRTIO_IOMMU_MAP_F_READ : 0) |
  609. (prot & IOMMU_WRITE ? VIRTIO_IOMMU_MAP_F_WRITE : 0) |
  610. (prot & IOMMU_MMIO ? VIRTIO_IOMMU_MAP_F_MMIO : 0);
  611. if (flags & ~vdomain->map_flags)
  612. return -EINVAL;
  613. ret = viommu_add_mapping(vdomain, iova, paddr, size, flags);
  614. if (ret)
  615. return ret;
  616. map = (struct virtio_iommu_req_map) {
  617. .head.type = VIRTIO_IOMMU_T_MAP,
  618. .domain = cpu_to_le32(vdomain->id),
  619. .virt_start = cpu_to_le64(iova),
  620. .phys_start = cpu_to_le64(paddr),
  621. .virt_end = cpu_to_le64(iova + size - 1),
  622. .flags = cpu_to_le32(flags),
  623. };
  624. if (!vdomain->nr_endpoints)
  625. return 0;
  626. ret = viommu_send_req_sync(vdomain->viommu, &map, sizeof(map));
  627. if (ret)
  628. viommu_del_mappings(vdomain, iova, size);
  629. return ret;
  630. }
  631. static size_t viommu_unmap(struct iommu_domain *domain, unsigned long iova,
  632. size_t size, struct iommu_iotlb_gather *gather)
  633. {
  634. int ret = 0;
  635. size_t unmapped;
  636. struct virtio_iommu_req_unmap unmap;
  637. struct viommu_domain *vdomain = to_viommu_domain(domain);
  638. unmapped = viommu_del_mappings(vdomain, iova, size);
  639. if (unmapped < size)
  640. return 0;
  641. /* Device already removed all mappings after detach. */
  642. if (!vdomain->nr_endpoints)
  643. return unmapped;
  644. unmap = (struct virtio_iommu_req_unmap) {
  645. .head.type = VIRTIO_IOMMU_T_UNMAP,
  646. .domain = cpu_to_le32(vdomain->id),
  647. .virt_start = cpu_to_le64(iova),
  648. .virt_end = cpu_to_le64(iova + unmapped - 1),
  649. };
  650. ret = viommu_add_req(vdomain->viommu, &unmap, sizeof(unmap));
  651. return ret ? 0 : unmapped;
  652. }
  653. static phys_addr_t viommu_iova_to_phys(struct iommu_domain *domain,
  654. dma_addr_t iova)
  655. {
  656. u64 paddr = 0;
  657. unsigned long flags;
  658. struct viommu_mapping *mapping;
  659. struct interval_tree_node *node;
  660. struct viommu_domain *vdomain = to_viommu_domain(domain);
  661. spin_lock_irqsave(&vdomain->mappings_lock, flags);
  662. node = interval_tree_iter_first(&vdomain->mappings, iova, iova);
  663. if (node) {
  664. mapping = container_of(node, struct viommu_mapping, iova);
  665. paddr = mapping->paddr + (iova - mapping->iova.start);
  666. }
  667. spin_unlock_irqrestore(&vdomain->mappings_lock, flags);
  668. return paddr;
  669. }
  670. static void viommu_iotlb_sync(struct iommu_domain *domain,
  671. struct iommu_iotlb_gather *gather)
  672. {
  673. struct viommu_domain *vdomain = to_viommu_domain(domain);
  674. viommu_sync_req(vdomain->viommu);
  675. }
  676. static void viommu_get_resv_regions(struct device *dev, struct list_head *head)
  677. {
  678. struct iommu_resv_region *entry, *new_entry, *msi = NULL;
  679. struct viommu_endpoint *vdev = dev_iommu_priv_get(dev);
  680. int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
  681. list_for_each_entry(entry, &vdev->resv_regions, list) {
  682. if (entry->type == IOMMU_RESV_MSI)
  683. msi = entry;
  684. new_entry = kmemdup(entry, sizeof(*entry), GFP_KERNEL);
  685. if (!new_entry)
  686. return;
  687. list_add_tail(&new_entry->list, head);
  688. }
  689. /*
  690. * If the device didn't register any bypass MSI window, add a
  691. * software-mapped region.
  692. */
  693. if (!msi) {
  694. msi = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH,
  695. prot, IOMMU_RESV_SW_MSI);
  696. if (!msi)
  697. return;
  698. list_add_tail(&msi->list, head);
  699. }
  700. iommu_dma_get_resv_regions(dev, head);
  701. }
  702. static struct iommu_ops viommu_ops;
  703. static struct virtio_driver virtio_iommu_drv;
  704. static int viommu_match_node(struct device *dev, const void *data)
  705. {
  706. return dev->parent->fwnode == data;
  707. }
  708. static struct viommu_dev *viommu_get_by_fwnode(struct fwnode_handle *fwnode)
  709. {
  710. struct device *dev = driver_find_device(&virtio_iommu_drv.driver, NULL,
  711. fwnode, viommu_match_node);
  712. put_device(dev);
  713. return dev ? dev_to_virtio(dev)->priv : NULL;
  714. }
  715. static struct iommu_device *viommu_probe_device(struct device *dev)
  716. {
  717. int ret;
  718. struct viommu_endpoint *vdev;
  719. struct viommu_dev *viommu = NULL;
  720. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  721. if (!fwspec || fwspec->ops != &viommu_ops)
  722. return ERR_PTR(-ENODEV);
  723. viommu = viommu_get_by_fwnode(fwspec->iommu_fwnode);
  724. if (!viommu)
  725. return ERR_PTR(-ENODEV);
  726. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  727. if (!vdev)
  728. return ERR_PTR(-ENOMEM);
  729. vdev->dev = dev;
  730. vdev->viommu = viommu;
  731. INIT_LIST_HEAD(&vdev->resv_regions);
  732. dev_iommu_priv_set(dev, vdev);
  733. if (viommu->probe_size) {
  734. /* Get additional information for this endpoint */
  735. ret = viommu_probe_endpoint(viommu, dev);
  736. if (ret)
  737. goto err_free_dev;
  738. }
  739. return &viommu->iommu;
  740. err_free_dev:
  741. generic_iommu_put_resv_regions(dev, &vdev->resv_regions);
  742. kfree(vdev);
  743. return ERR_PTR(ret);
  744. }
  745. static void viommu_release_device(struct device *dev)
  746. {
  747. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  748. struct viommu_endpoint *vdev;
  749. if (!fwspec || fwspec->ops != &viommu_ops)
  750. return;
  751. vdev = dev_iommu_priv_get(dev);
  752. generic_iommu_put_resv_regions(dev, &vdev->resv_regions);
  753. kfree(vdev);
  754. }
  755. static struct iommu_group *viommu_device_group(struct device *dev)
  756. {
  757. if (dev_is_pci(dev))
  758. return pci_device_group(dev);
  759. else
  760. return generic_device_group(dev);
  761. }
  762. static int viommu_of_xlate(struct device *dev, struct of_phandle_args *args)
  763. {
  764. return iommu_fwspec_add_ids(dev, args->args, 1);
  765. }
  766. static struct iommu_ops viommu_ops = {
  767. .domain_alloc = viommu_domain_alloc,
  768. .domain_free = viommu_domain_free,
  769. .attach_dev = viommu_attach_dev,
  770. .map = viommu_map,
  771. .unmap = viommu_unmap,
  772. .iova_to_phys = viommu_iova_to_phys,
  773. .iotlb_sync = viommu_iotlb_sync,
  774. .probe_device = viommu_probe_device,
  775. .release_device = viommu_release_device,
  776. .device_group = viommu_device_group,
  777. .get_resv_regions = viommu_get_resv_regions,
  778. .put_resv_regions = generic_iommu_put_resv_regions,
  779. .of_xlate = viommu_of_xlate,
  780. };
  781. static int viommu_init_vqs(struct viommu_dev *viommu)
  782. {
  783. struct virtio_device *vdev = dev_to_virtio(viommu->dev);
  784. const char *names[] = { "request", "event" };
  785. vq_callback_t *callbacks[] = {
  786. NULL, /* No async requests */
  787. viommu_event_handler,
  788. };
  789. return virtio_find_vqs(vdev, VIOMMU_NR_VQS, viommu->vqs, callbacks,
  790. names, NULL);
  791. }
  792. static int viommu_fill_evtq(struct viommu_dev *viommu)
  793. {
  794. int i, ret;
  795. struct scatterlist sg[1];
  796. struct viommu_event *evts;
  797. struct virtqueue *vq = viommu->vqs[VIOMMU_EVENT_VQ];
  798. size_t nr_evts = vq->num_free;
  799. viommu->evts = evts = devm_kmalloc_array(viommu->dev, nr_evts,
  800. sizeof(*evts), GFP_KERNEL);
  801. if (!evts)
  802. return -ENOMEM;
  803. for (i = 0; i < nr_evts; i++) {
  804. sg_init_one(sg, &evts[i], sizeof(*evts));
  805. ret = virtqueue_add_inbuf(vq, sg, 1, &evts[i], GFP_KERNEL);
  806. if (ret)
  807. return ret;
  808. }
  809. return 0;
  810. }
  811. static int viommu_probe(struct virtio_device *vdev)
  812. {
  813. struct device *parent_dev = vdev->dev.parent;
  814. struct viommu_dev *viommu = NULL;
  815. struct device *dev = &vdev->dev;
  816. u64 input_start = 0;
  817. u64 input_end = -1UL;
  818. int ret;
  819. if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
  820. !virtio_has_feature(vdev, VIRTIO_IOMMU_F_MAP_UNMAP))
  821. return -ENODEV;
  822. viommu = devm_kzalloc(dev, sizeof(*viommu), GFP_KERNEL);
  823. if (!viommu)
  824. return -ENOMEM;
  825. spin_lock_init(&viommu->request_lock);
  826. ida_init(&viommu->domain_ids);
  827. viommu->dev = dev;
  828. viommu->vdev = vdev;
  829. INIT_LIST_HEAD(&viommu->requests);
  830. ret = viommu_init_vqs(viommu);
  831. if (ret)
  832. return ret;
  833. virtio_cread_le(vdev, struct virtio_iommu_config, page_size_mask,
  834. &viommu->pgsize_bitmap);
  835. if (!viommu->pgsize_bitmap) {
  836. ret = -EINVAL;
  837. goto err_free_vqs;
  838. }
  839. viommu->map_flags = VIRTIO_IOMMU_MAP_F_READ | VIRTIO_IOMMU_MAP_F_WRITE;
  840. viommu->last_domain = ~0U;
  841. /* Optional features */
  842. virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_INPUT_RANGE,
  843. struct virtio_iommu_config, input_range.start,
  844. &input_start);
  845. virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_INPUT_RANGE,
  846. struct virtio_iommu_config, input_range.end,
  847. &input_end);
  848. virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_DOMAIN_RANGE,
  849. struct virtio_iommu_config, domain_range.start,
  850. &viommu->first_domain);
  851. virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_DOMAIN_RANGE,
  852. struct virtio_iommu_config, domain_range.end,
  853. &viommu->last_domain);
  854. virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_PROBE,
  855. struct virtio_iommu_config, probe_size,
  856. &viommu->probe_size);
  857. viommu->geometry = (struct iommu_domain_geometry) {
  858. .aperture_start = input_start,
  859. .aperture_end = input_end,
  860. .force_aperture = true,
  861. };
  862. if (virtio_has_feature(vdev, VIRTIO_IOMMU_F_MMIO))
  863. viommu->map_flags |= VIRTIO_IOMMU_MAP_F_MMIO;
  864. viommu_ops.pgsize_bitmap = viommu->pgsize_bitmap;
  865. virtio_device_ready(vdev);
  866. /* Populate the event queue with buffers */
  867. ret = viommu_fill_evtq(viommu);
  868. if (ret)
  869. goto err_free_vqs;
  870. ret = iommu_device_sysfs_add(&viommu->iommu, dev, NULL, "%s",
  871. virtio_bus_name(vdev));
  872. if (ret)
  873. goto err_free_vqs;
  874. iommu_device_set_ops(&viommu->iommu, &viommu_ops);
  875. iommu_device_set_fwnode(&viommu->iommu, parent_dev->fwnode);
  876. iommu_device_register(&viommu->iommu);
  877. #ifdef CONFIG_PCI
  878. if (pci_bus_type.iommu_ops != &viommu_ops) {
  879. ret = bus_set_iommu(&pci_bus_type, &viommu_ops);
  880. if (ret)
  881. goto err_unregister;
  882. }
  883. #endif
  884. #ifdef CONFIG_ARM_AMBA
  885. if (amba_bustype.iommu_ops != &viommu_ops) {
  886. ret = bus_set_iommu(&amba_bustype, &viommu_ops);
  887. if (ret)
  888. goto err_unregister;
  889. }
  890. #endif
  891. if (platform_bus_type.iommu_ops != &viommu_ops) {
  892. ret = bus_set_iommu(&platform_bus_type, &viommu_ops);
  893. if (ret)
  894. goto err_unregister;
  895. }
  896. vdev->priv = viommu;
  897. dev_info(dev, "input address: %u bits\n",
  898. order_base_2(viommu->geometry.aperture_end));
  899. dev_info(dev, "page mask: %#llx\n", viommu->pgsize_bitmap);
  900. return 0;
  901. err_unregister:
  902. iommu_device_sysfs_remove(&viommu->iommu);
  903. iommu_device_unregister(&viommu->iommu);
  904. err_free_vqs:
  905. vdev->config->del_vqs(vdev);
  906. return ret;
  907. }
  908. static void viommu_remove(struct virtio_device *vdev)
  909. {
  910. struct viommu_dev *viommu = vdev->priv;
  911. iommu_device_sysfs_remove(&viommu->iommu);
  912. iommu_device_unregister(&viommu->iommu);
  913. /* Stop all virtqueues */
  914. vdev->config->reset(vdev);
  915. vdev->config->del_vqs(vdev);
  916. dev_info(&vdev->dev, "device removed\n");
  917. }
  918. static void viommu_config_changed(struct virtio_device *vdev)
  919. {
  920. dev_warn(&vdev->dev, "config changed\n");
  921. }
  922. static unsigned int features[] = {
  923. VIRTIO_IOMMU_F_MAP_UNMAP,
  924. VIRTIO_IOMMU_F_INPUT_RANGE,
  925. VIRTIO_IOMMU_F_DOMAIN_RANGE,
  926. VIRTIO_IOMMU_F_PROBE,
  927. VIRTIO_IOMMU_F_MMIO,
  928. };
  929. static struct virtio_device_id id_table[] = {
  930. { VIRTIO_ID_IOMMU, VIRTIO_DEV_ANY_ID },
  931. { 0 },
  932. };
  933. MODULE_DEVICE_TABLE(virtio, id_table);
  934. static struct virtio_driver virtio_iommu_drv = {
  935. .driver.name = KBUILD_MODNAME,
  936. .driver.owner = THIS_MODULE,
  937. .id_table = id_table,
  938. .feature_table = features,
  939. .feature_table_size = ARRAY_SIZE(features),
  940. .probe = viommu_probe,
  941. .remove = viommu_remove,
  942. .config_changed = viommu_config_changed,
  943. };
  944. module_virtio_driver(virtio_iommu_drv);
  945. MODULE_DESCRIPTION("Virtio IOMMU driver");
  946. MODULE_AUTHOR("Jean-Philippe Brucker <jean-philippe.brucker@arm.com>");
  947. MODULE_LICENSE("GPL v2");