virtio-iommu.c 32 KB

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