virtio-iommu.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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. /* Supported MAP flags */
  42. u32 map_flags;
  43. u32 probe_size;
  44. };
  45. struct viommu_mapping {
  46. phys_addr_t paddr;
  47. struct interval_tree_node iova;
  48. u32 flags;
  49. };
  50. struct viommu_domain {
  51. struct iommu_domain domain;
  52. struct viommu_dev *viommu;
  53. struct mutex mutex; /* protects viommu pointer */
  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. bool bypass;
  60. };
  61. struct viommu_endpoint {
  62. struct device *dev;
  63. struct viommu_dev *viommu;
  64. struct viommu_domain *vdomain;
  65. struct list_head resv_regions;
  66. };
  67. struct viommu_request {
  68. struct list_head list;
  69. void *writeback;
  70. unsigned int write_offset;
  71. unsigned int len;
  72. char buf[] __counted_by(len);
  73. };
  74. #define VIOMMU_FAULT_RESV_MASK 0xffffff00
  75. struct viommu_event {
  76. union {
  77. u32 head;
  78. struct virtio_iommu_fault fault;
  79. };
  80. };
  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(struct_size(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. /*
  254. * viommu_add_mapping - add a mapping to the internal tree
  255. *
  256. * On success, return the new mapping. Otherwise return NULL.
  257. */
  258. static int viommu_add_mapping(struct viommu_domain *vdomain, u64 iova, u64 end,
  259. phys_addr_t paddr, u32 flags)
  260. {
  261. unsigned long irqflags;
  262. struct viommu_mapping *mapping;
  263. mapping = kzalloc(sizeof(*mapping), GFP_ATOMIC);
  264. if (!mapping)
  265. return -ENOMEM;
  266. mapping->paddr = paddr;
  267. mapping->iova.start = iova;
  268. mapping->iova.last = end;
  269. mapping->flags = flags;
  270. spin_lock_irqsave(&vdomain->mappings_lock, irqflags);
  271. interval_tree_insert(&mapping->iova, &vdomain->mappings);
  272. spin_unlock_irqrestore(&vdomain->mappings_lock, irqflags);
  273. return 0;
  274. }
  275. /*
  276. * viommu_del_mappings - remove mappings from the internal tree
  277. *
  278. * @vdomain: the domain
  279. * @iova: start of the range
  280. * @end: end of the range
  281. *
  282. * On success, returns the number of unmapped bytes
  283. */
  284. static size_t viommu_del_mappings(struct viommu_domain *vdomain,
  285. u64 iova, u64 end)
  286. {
  287. size_t unmapped = 0;
  288. unsigned long flags;
  289. struct viommu_mapping *mapping = NULL;
  290. struct interval_tree_node *node, *next;
  291. spin_lock_irqsave(&vdomain->mappings_lock, flags);
  292. next = interval_tree_iter_first(&vdomain->mappings, iova, end);
  293. while (next) {
  294. node = next;
  295. mapping = container_of(node, struct viommu_mapping, iova);
  296. next = interval_tree_iter_next(node, iova, end);
  297. /* Trying to split a mapping? */
  298. if (mapping->iova.start < iova)
  299. break;
  300. /*
  301. * Virtio-iommu doesn't allow UNMAP to split a mapping created
  302. * with a single MAP request, so remove the full mapping.
  303. */
  304. unmapped += mapping->iova.last - mapping->iova.start + 1;
  305. interval_tree_remove(node, &vdomain->mappings);
  306. kfree(mapping);
  307. }
  308. spin_unlock_irqrestore(&vdomain->mappings_lock, flags);
  309. return unmapped;
  310. }
  311. /*
  312. * Fill the domain with identity mappings, skipping the device's reserved
  313. * regions.
  314. */
  315. static int viommu_domain_map_identity(struct viommu_endpoint *vdev,
  316. struct viommu_domain *vdomain)
  317. {
  318. int ret;
  319. struct iommu_resv_region *resv;
  320. u64 iova = vdomain->domain.geometry.aperture_start;
  321. u64 limit = vdomain->domain.geometry.aperture_end;
  322. u32 flags = VIRTIO_IOMMU_MAP_F_READ | VIRTIO_IOMMU_MAP_F_WRITE;
  323. unsigned long granule = 1UL << __ffs(vdomain->domain.pgsize_bitmap);
  324. iova = ALIGN(iova, granule);
  325. limit = ALIGN_DOWN(limit + 1, granule) - 1;
  326. list_for_each_entry(resv, &vdev->resv_regions, list) {
  327. u64 resv_start = ALIGN_DOWN(resv->start, granule);
  328. u64 resv_end = ALIGN(resv->start + resv->length, granule) - 1;
  329. if (resv_end < iova || resv_start > limit)
  330. /* No overlap */
  331. continue;
  332. if (resv_start > iova) {
  333. ret = viommu_add_mapping(vdomain, iova, resv_start - 1,
  334. (phys_addr_t)iova, flags);
  335. if (ret)
  336. goto err_unmap;
  337. }
  338. if (resv_end >= limit)
  339. return 0;
  340. iova = resv_end + 1;
  341. }
  342. ret = viommu_add_mapping(vdomain, iova, limit, (phys_addr_t)iova,
  343. flags);
  344. if (ret)
  345. goto err_unmap;
  346. return 0;
  347. err_unmap:
  348. viommu_del_mappings(vdomain, 0, iova);
  349. return ret;
  350. }
  351. /*
  352. * viommu_replay_mappings - re-send MAP requests
  353. *
  354. * When reattaching a domain that was previously detached from all endpoints,
  355. * mappings were deleted from the device. Re-create the mappings available in
  356. * the internal tree.
  357. */
  358. static int viommu_replay_mappings(struct viommu_domain *vdomain)
  359. {
  360. int ret = 0;
  361. unsigned long flags;
  362. struct viommu_mapping *mapping;
  363. struct interval_tree_node *node;
  364. struct virtio_iommu_req_map map;
  365. spin_lock_irqsave(&vdomain->mappings_lock, flags);
  366. node = interval_tree_iter_first(&vdomain->mappings, 0, -1UL);
  367. while (node) {
  368. mapping = container_of(node, struct viommu_mapping, iova);
  369. map = (struct virtio_iommu_req_map) {
  370. .head.type = VIRTIO_IOMMU_T_MAP,
  371. .domain = cpu_to_le32(vdomain->id),
  372. .virt_start = cpu_to_le64(mapping->iova.start),
  373. .virt_end = cpu_to_le64(mapping->iova.last),
  374. .phys_start = cpu_to_le64(mapping->paddr),
  375. .flags = cpu_to_le32(mapping->flags),
  376. };
  377. ret = viommu_send_req_sync(vdomain->viommu, &map, sizeof(map));
  378. if (ret)
  379. break;
  380. node = interval_tree_iter_next(node, 0, -1UL);
  381. }
  382. spin_unlock_irqrestore(&vdomain->mappings_lock, flags);
  383. return ret;
  384. }
  385. static int viommu_add_resv_mem(struct viommu_endpoint *vdev,
  386. struct virtio_iommu_probe_resv_mem *mem,
  387. size_t len)
  388. {
  389. size_t size;
  390. u64 start64, end64;
  391. phys_addr_t start, end;
  392. struct iommu_resv_region *region = NULL, *next;
  393. unsigned long prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
  394. start = start64 = le64_to_cpu(mem->start);
  395. end = end64 = le64_to_cpu(mem->end);
  396. size = end64 - start64 + 1;
  397. /* Catch any overflow, including the unlikely end64 - start64 + 1 = 0 */
  398. if (start != start64 || end != end64 || size < end64 - start64)
  399. return -EOVERFLOW;
  400. if (len < sizeof(*mem))
  401. return -EINVAL;
  402. switch (mem->subtype) {
  403. default:
  404. dev_warn(vdev->dev, "unknown resv mem subtype 0x%x\n",
  405. mem->subtype);
  406. fallthrough;
  407. case VIRTIO_IOMMU_RESV_MEM_T_RESERVED:
  408. region = iommu_alloc_resv_region(start, size, 0,
  409. IOMMU_RESV_RESERVED,
  410. GFP_KERNEL);
  411. break;
  412. case VIRTIO_IOMMU_RESV_MEM_T_MSI:
  413. region = iommu_alloc_resv_region(start, size, prot,
  414. IOMMU_RESV_MSI,
  415. GFP_KERNEL);
  416. break;
  417. }
  418. if (!region)
  419. return -ENOMEM;
  420. /* Keep the list sorted */
  421. list_for_each_entry(next, &vdev->resv_regions, list) {
  422. if (next->start > region->start)
  423. break;
  424. }
  425. list_add_tail(&region->list, &next->list);
  426. return 0;
  427. }
  428. static int viommu_probe_endpoint(struct viommu_dev *viommu, struct device *dev)
  429. {
  430. int ret;
  431. u16 type, len;
  432. size_t cur = 0;
  433. size_t probe_len;
  434. struct virtio_iommu_req_probe *probe;
  435. struct virtio_iommu_probe_property *prop;
  436. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  437. struct viommu_endpoint *vdev = dev_iommu_priv_get(dev);
  438. if (!fwspec->num_ids)
  439. return -EINVAL;
  440. probe_len = sizeof(*probe) + viommu->probe_size +
  441. sizeof(struct virtio_iommu_req_tail);
  442. probe = kzalloc(probe_len, GFP_KERNEL);
  443. if (!probe)
  444. return -ENOMEM;
  445. probe->head.type = VIRTIO_IOMMU_T_PROBE;
  446. /*
  447. * For now, assume that properties of an endpoint that outputs multiple
  448. * IDs are consistent. Only probe the first one.
  449. */
  450. probe->endpoint = cpu_to_le32(fwspec->ids[0]);
  451. ret = viommu_send_req_sync(viommu, probe, probe_len);
  452. if (ret)
  453. goto out_free;
  454. prop = (void *)probe->properties;
  455. type = le16_to_cpu(prop->type) & VIRTIO_IOMMU_PROBE_T_MASK;
  456. while (type != VIRTIO_IOMMU_PROBE_T_NONE &&
  457. cur < viommu->probe_size) {
  458. len = le16_to_cpu(prop->length) + sizeof(*prop);
  459. switch (type) {
  460. case VIRTIO_IOMMU_PROBE_T_RESV_MEM:
  461. ret = viommu_add_resv_mem(vdev, (void *)prop, len);
  462. break;
  463. default:
  464. dev_err(dev, "unknown viommu prop 0x%x\n", type);
  465. }
  466. if (ret)
  467. dev_err(dev, "failed to parse viommu prop 0x%x\n", type);
  468. cur += len;
  469. if (cur >= viommu->probe_size)
  470. break;
  471. prop = (void *)probe->properties + cur;
  472. type = le16_to_cpu(prop->type) & VIRTIO_IOMMU_PROBE_T_MASK;
  473. }
  474. out_free:
  475. kfree(probe);
  476. return ret;
  477. }
  478. static int viommu_fault_handler(struct viommu_dev *viommu,
  479. struct virtio_iommu_fault *fault)
  480. {
  481. char *reason_str;
  482. u8 reason = fault->reason;
  483. u32 flags = le32_to_cpu(fault->flags);
  484. u32 endpoint = le32_to_cpu(fault->endpoint);
  485. u64 address = le64_to_cpu(fault->address);
  486. switch (reason) {
  487. case VIRTIO_IOMMU_FAULT_R_DOMAIN:
  488. reason_str = "domain";
  489. break;
  490. case VIRTIO_IOMMU_FAULT_R_MAPPING:
  491. reason_str = "page";
  492. break;
  493. case VIRTIO_IOMMU_FAULT_R_UNKNOWN:
  494. default:
  495. reason_str = "unknown";
  496. break;
  497. }
  498. /* TODO: find EP by ID and report_iommu_fault */
  499. if (flags & VIRTIO_IOMMU_FAULT_F_ADDRESS)
  500. dev_err_ratelimited(viommu->dev, "%s fault from EP %u at %#llx [%s%s%s]\n",
  501. reason_str, endpoint, address,
  502. flags & VIRTIO_IOMMU_FAULT_F_READ ? "R" : "",
  503. flags & VIRTIO_IOMMU_FAULT_F_WRITE ? "W" : "",
  504. flags & VIRTIO_IOMMU_FAULT_F_EXEC ? "X" : "");
  505. else
  506. dev_err_ratelimited(viommu->dev, "%s fault from EP %u\n",
  507. reason_str, endpoint);
  508. return 0;
  509. }
  510. static void viommu_event_handler(struct virtqueue *vq)
  511. {
  512. int ret;
  513. unsigned int len;
  514. struct scatterlist sg[1];
  515. struct viommu_event *evt;
  516. struct viommu_dev *viommu = vq->vdev->priv;
  517. while ((evt = virtqueue_get_buf(vq, &len)) != NULL) {
  518. if (len > sizeof(*evt)) {
  519. dev_err(viommu->dev,
  520. "invalid event buffer (len %u != %zu)\n",
  521. len, sizeof(*evt));
  522. } else if (!(evt->head & VIOMMU_FAULT_RESV_MASK)) {
  523. viommu_fault_handler(viommu, &evt->fault);
  524. }
  525. sg_init_one(sg, evt, sizeof(*evt));
  526. ret = virtqueue_add_inbuf(vq, sg, 1, evt, GFP_ATOMIC);
  527. if (ret)
  528. dev_err(viommu->dev, "could not add event buffer\n");
  529. }
  530. virtqueue_kick(vq);
  531. }
  532. /* IOMMU API */
  533. static struct iommu_domain *viommu_domain_alloc(unsigned type)
  534. {
  535. struct viommu_domain *vdomain;
  536. if (type != IOMMU_DOMAIN_UNMANAGED &&
  537. type != IOMMU_DOMAIN_DMA &&
  538. type != IOMMU_DOMAIN_IDENTITY)
  539. return NULL;
  540. vdomain = kzalloc(sizeof(*vdomain), GFP_KERNEL);
  541. if (!vdomain)
  542. return NULL;
  543. mutex_init(&vdomain->mutex);
  544. spin_lock_init(&vdomain->mappings_lock);
  545. vdomain->mappings = RB_ROOT_CACHED;
  546. return &vdomain->domain;
  547. }
  548. static int viommu_domain_finalise(struct viommu_endpoint *vdev,
  549. struct iommu_domain *domain)
  550. {
  551. int ret;
  552. unsigned long viommu_page_size;
  553. struct viommu_dev *viommu = vdev->viommu;
  554. struct viommu_domain *vdomain = to_viommu_domain(domain);
  555. viommu_page_size = 1UL << __ffs(viommu->pgsize_bitmap);
  556. if (viommu_page_size > PAGE_SIZE) {
  557. dev_err(vdev->dev,
  558. "granule 0x%lx larger than system page size 0x%lx\n",
  559. viommu_page_size, PAGE_SIZE);
  560. return -ENODEV;
  561. }
  562. ret = ida_alloc_range(&viommu->domain_ids, viommu->first_domain,
  563. viommu->last_domain, GFP_KERNEL);
  564. if (ret < 0)
  565. return ret;
  566. vdomain->id = (unsigned int)ret;
  567. domain->pgsize_bitmap = viommu->pgsize_bitmap;
  568. domain->geometry = viommu->geometry;
  569. vdomain->map_flags = viommu->map_flags;
  570. vdomain->viommu = viommu;
  571. if (domain->type == IOMMU_DOMAIN_IDENTITY) {
  572. if (virtio_has_feature(viommu->vdev,
  573. VIRTIO_IOMMU_F_BYPASS_CONFIG)) {
  574. vdomain->bypass = true;
  575. return 0;
  576. }
  577. ret = viommu_domain_map_identity(vdev, vdomain);
  578. if (ret) {
  579. ida_free(&viommu->domain_ids, vdomain->id);
  580. vdomain->viommu = NULL;
  581. return ret;
  582. }
  583. }
  584. return 0;
  585. }
  586. static void viommu_domain_free(struct iommu_domain *domain)
  587. {
  588. struct viommu_domain *vdomain = to_viommu_domain(domain);
  589. /* Free all remaining mappings */
  590. viommu_del_mappings(vdomain, 0, ULLONG_MAX);
  591. if (vdomain->viommu)
  592. ida_free(&vdomain->viommu->domain_ids, vdomain->id);
  593. kfree(vdomain);
  594. }
  595. static int viommu_attach_dev(struct iommu_domain *domain, struct device *dev)
  596. {
  597. int i;
  598. int ret = 0;
  599. struct virtio_iommu_req_attach req;
  600. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  601. struct viommu_endpoint *vdev = dev_iommu_priv_get(dev);
  602. struct viommu_domain *vdomain = to_viommu_domain(domain);
  603. mutex_lock(&vdomain->mutex);
  604. if (!vdomain->viommu) {
  605. /*
  606. * Properly initialize the domain now that we know which viommu
  607. * owns it.
  608. */
  609. ret = viommu_domain_finalise(vdev, domain);
  610. } else if (vdomain->viommu != vdev->viommu) {
  611. ret = -EINVAL;
  612. }
  613. mutex_unlock(&vdomain->mutex);
  614. if (ret)
  615. return ret;
  616. /*
  617. * In the virtio-iommu device, when attaching the endpoint to a new
  618. * domain, it is detached from the old one and, if as a result the
  619. * old domain isn't attached to any endpoint, all mappings are removed
  620. * from the old domain and it is freed.
  621. *
  622. * In the driver the old domain still exists, and its mappings will be
  623. * recreated if it gets reattached to an endpoint. Otherwise it will be
  624. * freed explicitly.
  625. *
  626. * vdev->vdomain is protected by group->mutex
  627. */
  628. if (vdev->vdomain)
  629. vdev->vdomain->nr_endpoints--;
  630. req = (struct virtio_iommu_req_attach) {
  631. .head.type = VIRTIO_IOMMU_T_ATTACH,
  632. .domain = cpu_to_le32(vdomain->id),
  633. };
  634. if (vdomain->bypass)
  635. req.flags |= cpu_to_le32(VIRTIO_IOMMU_ATTACH_F_BYPASS);
  636. for (i = 0; i < fwspec->num_ids; i++) {
  637. req.endpoint = cpu_to_le32(fwspec->ids[i]);
  638. ret = viommu_send_req_sync(vdomain->viommu, &req, sizeof(req));
  639. if (ret)
  640. return ret;
  641. }
  642. if (!vdomain->nr_endpoints) {
  643. /*
  644. * This endpoint is the first to be attached to the domain.
  645. * Replay existing mappings (e.g. SW MSI).
  646. */
  647. ret = viommu_replay_mappings(vdomain);
  648. if (ret)
  649. return ret;
  650. }
  651. vdomain->nr_endpoints++;
  652. vdev->vdomain = vdomain;
  653. return 0;
  654. }
  655. static void viommu_detach_dev(struct viommu_endpoint *vdev)
  656. {
  657. int i;
  658. struct virtio_iommu_req_detach req;
  659. struct viommu_domain *vdomain = vdev->vdomain;
  660. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(vdev->dev);
  661. if (!vdomain)
  662. return;
  663. req = (struct virtio_iommu_req_detach) {
  664. .head.type = VIRTIO_IOMMU_T_DETACH,
  665. .domain = cpu_to_le32(vdomain->id),
  666. };
  667. for (i = 0; i < fwspec->num_ids; i++) {
  668. req.endpoint = cpu_to_le32(fwspec->ids[i]);
  669. WARN_ON(viommu_send_req_sync(vdev->viommu, &req, sizeof(req)));
  670. }
  671. vdomain->nr_endpoints--;
  672. vdev->vdomain = NULL;
  673. }
  674. static int viommu_map_pages(struct iommu_domain *domain, unsigned long iova,
  675. phys_addr_t paddr, size_t pgsize, size_t pgcount,
  676. int prot, gfp_t gfp, size_t *mapped)
  677. {
  678. int ret;
  679. u32 flags;
  680. size_t size = pgsize * pgcount;
  681. u64 end = iova + size - 1;
  682. struct virtio_iommu_req_map map;
  683. struct viommu_domain *vdomain = to_viommu_domain(domain);
  684. flags = (prot & IOMMU_READ ? VIRTIO_IOMMU_MAP_F_READ : 0) |
  685. (prot & IOMMU_WRITE ? VIRTIO_IOMMU_MAP_F_WRITE : 0) |
  686. (prot & IOMMU_MMIO ? VIRTIO_IOMMU_MAP_F_MMIO : 0);
  687. if (flags & ~vdomain->map_flags)
  688. return -EINVAL;
  689. ret = viommu_add_mapping(vdomain, iova, end, paddr, flags);
  690. if (ret)
  691. return ret;
  692. if (vdomain->nr_endpoints) {
  693. map = (struct virtio_iommu_req_map) {
  694. .head.type = VIRTIO_IOMMU_T_MAP,
  695. .domain = cpu_to_le32(vdomain->id),
  696. .virt_start = cpu_to_le64(iova),
  697. .phys_start = cpu_to_le64(paddr),
  698. .virt_end = cpu_to_le64(end),
  699. .flags = cpu_to_le32(flags),
  700. };
  701. ret = viommu_add_req(vdomain->viommu, &map, sizeof(map));
  702. if (ret) {
  703. viommu_del_mappings(vdomain, iova, end);
  704. return ret;
  705. }
  706. }
  707. if (mapped)
  708. *mapped = size;
  709. return 0;
  710. }
  711. static size_t viommu_unmap_pages(struct iommu_domain *domain, unsigned long iova,
  712. size_t pgsize, size_t pgcount,
  713. struct iommu_iotlb_gather *gather)
  714. {
  715. int ret = 0;
  716. size_t unmapped;
  717. struct virtio_iommu_req_unmap unmap;
  718. struct viommu_domain *vdomain = to_viommu_domain(domain);
  719. size_t size = pgsize * pgcount;
  720. unmapped = viommu_del_mappings(vdomain, iova, iova + size - 1);
  721. if (unmapped < size)
  722. return 0;
  723. /* Device already removed all mappings after detach. */
  724. if (!vdomain->nr_endpoints)
  725. return unmapped;
  726. unmap = (struct virtio_iommu_req_unmap) {
  727. .head.type = VIRTIO_IOMMU_T_UNMAP,
  728. .domain = cpu_to_le32(vdomain->id),
  729. .virt_start = cpu_to_le64(iova),
  730. .virt_end = cpu_to_le64(iova + unmapped - 1),
  731. };
  732. ret = viommu_add_req(vdomain->viommu, &unmap, sizeof(unmap));
  733. return ret ? 0 : unmapped;
  734. }
  735. static phys_addr_t viommu_iova_to_phys(struct iommu_domain *domain,
  736. dma_addr_t iova)
  737. {
  738. u64 paddr = 0;
  739. unsigned long flags;
  740. struct viommu_mapping *mapping;
  741. struct interval_tree_node *node;
  742. struct viommu_domain *vdomain = to_viommu_domain(domain);
  743. spin_lock_irqsave(&vdomain->mappings_lock, flags);
  744. node = interval_tree_iter_first(&vdomain->mappings, iova, iova);
  745. if (node) {
  746. mapping = container_of(node, struct viommu_mapping, iova);
  747. paddr = mapping->paddr + (iova - mapping->iova.start);
  748. }
  749. spin_unlock_irqrestore(&vdomain->mappings_lock, flags);
  750. return paddr;
  751. }
  752. static void viommu_iotlb_sync(struct iommu_domain *domain,
  753. struct iommu_iotlb_gather *gather)
  754. {
  755. struct viommu_domain *vdomain = to_viommu_domain(domain);
  756. viommu_sync_req(vdomain->viommu);
  757. }
  758. static int viommu_iotlb_sync_map(struct iommu_domain *domain,
  759. unsigned long iova, size_t size)
  760. {
  761. struct viommu_domain *vdomain = to_viommu_domain(domain);
  762. /*
  763. * May be called before the viommu is initialized including
  764. * while creating direct mapping
  765. */
  766. if (!vdomain->nr_endpoints)
  767. return 0;
  768. return viommu_sync_req(vdomain->viommu);
  769. }
  770. static void viommu_flush_iotlb_all(struct iommu_domain *domain)
  771. {
  772. struct viommu_domain *vdomain = to_viommu_domain(domain);
  773. /*
  774. * May be called before the viommu is initialized including
  775. * while creating direct mapping
  776. */
  777. if (!vdomain->nr_endpoints)
  778. return;
  779. viommu_sync_req(vdomain->viommu);
  780. }
  781. static void viommu_get_resv_regions(struct device *dev, struct list_head *head)
  782. {
  783. struct iommu_resv_region *entry, *new_entry, *msi = NULL;
  784. struct viommu_endpoint *vdev = dev_iommu_priv_get(dev);
  785. int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
  786. list_for_each_entry(entry, &vdev->resv_regions, list) {
  787. if (entry->type == IOMMU_RESV_MSI)
  788. msi = entry;
  789. new_entry = kmemdup(entry, sizeof(*entry), GFP_KERNEL);
  790. if (!new_entry)
  791. return;
  792. list_add_tail(&new_entry->list, head);
  793. }
  794. /*
  795. * If the device didn't register any bypass MSI window, add a
  796. * software-mapped region.
  797. */
  798. if (!msi) {
  799. msi = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH,
  800. prot, IOMMU_RESV_SW_MSI,
  801. GFP_KERNEL);
  802. if (!msi)
  803. return;
  804. list_add_tail(&msi->list, head);
  805. }
  806. iommu_dma_get_resv_regions(dev, head);
  807. }
  808. static struct iommu_ops viommu_ops;
  809. static struct virtio_driver virtio_iommu_drv;
  810. static int viommu_match_node(struct device *dev, const void *data)
  811. {
  812. return device_match_fwnode(dev->parent, data);
  813. }
  814. static struct viommu_dev *viommu_get_by_fwnode(struct fwnode_handle *fwnode)
  815. {
  816. struct device *dev = driver_find_device(&virtio_iommu_drv.driver, NULL,
  817. fwnode, viommu_match_node);
  818. put_device(dev);
  819. return dev ? dev_to_virtio(dev)->priv : NULL;
  820. }
  821. static struct iommu_device *viommu_probe_device(struct device *dev)
  822. {
  823. int ret;
  824. struct viommu_endpoint *vdev;
  825. struct viommu_dev *viommu = NULL;
  826. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  827. viommu = viommu_get_by_fwnode(fwspec->iommu_fwnode);
  828. if (!viommu)
  829. return ERR_PTR(-ENODEV);
  830. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  831. if (!vdev)
  832. return ERR_PTR(-ENOMEM);
  833. vdev->dev = dev;
  834. vdev->viommu = viommu;
  835. INIT_LIST_HEAD(&vdev->resv_regions);
  836. dev_iommu_priv_set(dev, vdev);
  837. if (viommu->probe_size) {
  838. /* Get additional information for this endpoint */
  839. ret = viommu_probe_endpoint(viommu, dev);
  840. if (ret)
  841. goto err_free_dev;
  842. }
  843. return &viommu->iommu;
  844. err_free_dev:
  845. iommu_put_resv_regions(dev, &vdev->resv_regions);
  846. kfree(vdev);
  847. return ERR_PTR(ret);
  848. }
  849. static void viommu_release_device(struct device *dev)
  850. {
  851. struct viommu_endpoint *vdev = dev_iommu_priv_get(dev);
  852. viommu_detach_dev(vdev);
  853. iommu_put_resv_regions(dev, &vdev->resv_regions);
  854. kfree(vdev);
  855. }
  856. static struct iommu_group *viommu_device_group(struct device *dev)
  857. {
  858. if (dev_is_pci(dev))
  859. return pci_device_group(dev);
  860. else
  861. return generic_device_group(dev);
  862. }
  863. static int viommu_of_xlate(struct device *dev,
  864. const struct of_phandle_args *args)
  865. {
  866. return iommu_fwspec_add_ids(dev, args->args, 1);
  867. }
  868. static bool viommu_capable(struct device *dev, enum iommu_cap cap)
  869. {
  870. switch (cap) {
  871. case IOMMU_CAP_CACHE_COHERENCY:
  872. return true;
  873. case IOMMU_CAP_DEFERRED_FLUSH:
  874. return true;
  875. default:
  876. return false;
  877. }
  878. }
  879. static struct iommu_ops viommu_ops = {
  880. .capable = viommu_capable,
  881. .domain_alloc = viommu_domain_alloc,
  882. .probe_device = viommu_probe_device,
  883. .release_device = viommu_release_device,
  884. .device_group = viommu_device_group,
  885. .get_resv_regions = viommu_get_resv_regions,
  886. .of_xlate = viommu_of_xlate,
  887. .owner = THIS_MODULE,
  888. .default_domain_ops = &(const struct iommu_domain_ops) {
  889. .attach_dev = viommu_attach_dev,
  890. .map_pages = viommu_map_pages,
  891. .unmap_pages = viommu_unmap_pages,
  892. .iova_to_phys = viommu_iova_to_phys,
  893. .flush_iotlb_all = viommu_flush_iotlb_all,
  894. .iotlb_sync = viommu_iotlb_sync,
  895. .iotlb_sync_map = viommu_iotlb_sync_map,
  896. .free = viommu_domain_free,
  897. }
  898. };
  899. static int viommu_init_vqs(struct viommu_dev *viommu)
  900. {
  901. struct virtio_device *vdev = dev_to_virtio(viommu->dev);
  902. struct virtqueue_info vqs_info[] = {
  903. { "request" },
  904. { "event", viommu_event_handler },
  905. };
  906. return virtio_find_vqs(vdev, VIOMMU_NR_VQS, viommu->vqs,
  907. vqs_info, NULL);
  908. }
  909. static int viommu_fill_evtq(struct viommu_dev *viommu)
  910. {
  911. int i, ret;
  912. struct scatterlist sg[1];
  913. struct viommu_event *evts;
  914. struct virtqueue *vq = viommu->vqs[VIOMMU_EVENT_VQ];
  915. size_t nr_evts = vq->num_free;
  916. viommu->evts = evts = devm_kmalloc_array(viommu->dev, nr_evts,
  917. sizeof(*evts), GFP_KERNEL);
  918. if (!evts)
  919. return -ENOMEM;
  920. for (i = 0; i < nr_evts; i++) {
  921. sg_init_one(sg, &evts[i], sizeof(*evts));
  922. ret = virtqueue_add_inbuf(vq, sg, 1, &evts[i], GFP_KERNEL);
  923. if (ret)
  924. return ret;
  925. }
  926. return 0;
  927. }
  928. static int viommu_probe(struct virtio_device *vdev)
  929. {
  930. struct device *parent_dev = vdev->dev.parent;
  931. struct viommu_dev *viommu = NULL;
  932. struct device *dev = &vdev->dev;
  933. u64 input_start = 0;
  934. u64 input_end = -1UL;
  935. int ret;
  936. if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
  937. !virtio_has_feature(vdev, VIRTIO_IOMMU_F_MAP_UNMAP))
  938. return -ENODEV;
  939. viommu = devm_kzalloc(dev, sizeof(*viommu), GFP_KERNEL);
  940. if (!viommu)
  941. return -ENOMEM;
  942. spin_lock_init(&viommu->request_lock);
  943. ida_init(&viommu->domain_ids);
  944. viommu->dev = dev;
  945. viommu->vdev = vdev;
  946. INIT_LIST_HEAD(&viommu->requests);
  947. ret = viommu_init_vqs(viommu);
  948. if (ret)
  949. return ret;
  950. virtio_cread_le(vdev, struct virtio_iommu_config, page_size_mask,
  951. &viommu->pgsize_bitmap);
  952. if (!viommu->pgsize_bitmap) {
  953. ret = -EINVAL;
  954. goto err_free_vqs;
  955. }
  956. viommu->map_flags = VIRTIO_IOMMU_MAP_F_READ | VIRTIO_IOMMU_MAP_F_WRITE;
  957. viommu->last_domain = ~0U;
  958. /* Optional features */
  959. virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_INPUT_RANGE,
  960. struct virtio_iommu_config, input_range.start,
  961. &input_start);
  962. virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_INPUT_RANGE,
  963. struct virtio_iommu_config, input_range.end,
  964. &input_end);
  965. virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_DOMAIN_RANGE,
  966. struct virtio_iommu_config, domain_range.start,
  967. &viommu->first_domain);
  968. virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_DOMAIN_RANGE,
  969. struct virtio_iommu_config, domain_range.end,
  970. &viommu->last_domain);
  971. virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_PROBE,
  972. struct virtio_iommu_config, probe_size,
  973. &viommu->probe_size);
  974. viommu->geometry = (struct iommu_domain_geometry) {
  975. .aperture_start = input_start,
  976. .aperture_end = input_end,
  977. .force_aperture = true,
  978. };
  979. if (virtio_has_feature(vdev, VIRTIO_IOMMU_F_MMIO))
  980. viommu->map_flags |= VIRTIO_IOMMU_MAP_F_MMIO;
  981. viommu_ops.pgsize_bitmap = viommu->pgsize_bitmap;
  982. virtio_device_ready(vdev);
  983. /* Populate the event queue with buffers */
  984. ret = viommu_fill_evtq(viommu);
  985. if (ret)
  986. goto err_free_vqs;
  987. ret = iommu_device_sysfs_add(&viommu->iommu, dev, NULL, "%s",
  988. virtio_bus_name(vdev));
  989. if (ret)
  990. goto err_free_vqs;
  991. iommu_device_register(&viommu->iommu, &viommu_ops, parent_dev);
  992. vdev->priv = viommu;
  993. dev_info(dev, "input address: %u bits\n",
  994. order_base_2(viommu->geometry.aperture_end));
  995. dev_info(dev, "page mask: %#llx\n", viommu->pgsize_bitmap);
  996. return 0;
  997. err_free_vqs:
  998. vdev->config->del_vqs(vdev);
  999. return ret;
  1000. }
  1001. static void viommu_remove(struct virtio_device *vdev)
  1002. {
  1003. struct viommu_dev *viommu = vdev->priv;
  1004. iommu_device_sysfs_remove(&viommu->iommu);
  1005. iommu_device_unregister(&viommu->iommu);
  1006. /* Stop all virtqueues */
  1007. virtio_reset_device(vdev);
  1008. vdev->config->del_vqs(vdev);
  1009. dev_info(&vdev->dev, "device removed\n");
  1010. }
  1011. static void viommu_config_changed(struct virtio_device *vdev)
  1012. {
  1013. dev_warn(&vdev->dev, "config changed\n");
  1014. }
  1015. static unsigned int features[] = {
  1016. VIRTIO_IOMMU_F_MAP_UNMAP,
  1017. VIRTIO_IOMMU_F_INPUT_RANGE,
  1018. VIRTIO_IOMMU_F_DOMAIN_RANGE,
  1019. VIRTIO_IOMMU_F_PROBE,
  1020. VIRTIO_IOMMU_F_MMIO,
  1021. VIRTIO_IOMMU_F_BYPASS_CONFIG,
  1022. };
  1023. static struct virtio_device_id id_table[] = {
  1024. { VIRTIO_ID_IOMMU, VIRTIO_DEV_ANY_ID },
  1025. { 0 },
  1026. };
  1027. MODULE_DEVICE_TABLE(virtio, id_table);
  1028. static struct virtio_driver virtio_iommu_drv = {
  1029. .driver.name = KBUILD_MODNAME,
  1030. .id_table = id_table,
  1031. .feature_table = features,
  1032. .feature_table_size = ARRAY_SIZE(features),
  1033. .probe = viommu_probe,
  1034. .remove = viommu_remove,
  1035. .config_changed = viommu_config_changed,
  1036. };
  1037. module_virtio_driver(virtio_iommu_drv);
  1038. MODULE_DESCRIPTION("Virtio IOMMU driver");
  1039. MODULE_AUTHOR("Jean-Philippe Brucker <jean-philippe.brucker@arm.com>");
  1040. MODULE_LICENSE("GPL v2");