amd_iommu.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. /*
  2. * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <joerg.roedel@amd.com>
  4. * Leo Duran <leo.duran@amd.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/pci.h>
  20. #include <linux/gfp.h>
  21. #include <linux/bitops.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/iommu-helper.h>
  24. #include <asm/proto.h>
  25. #include <asm/iommu.h>
  26. #include <asm/amd_iommu_types.h>
  27. #include <asm/amd_iommu.h>
  28. #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
  29. #define EXIT_LOOP_COUNT 10000000
  30. static DEFINE_RWLOCK(amd_iommu_devtable_lock);
  31. /*
  32. * general struct to manage commands send to an IOMMU
  33. */
  34. struct iommu_cmd {
  35. u32 data[4];
  36. };
  37. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  38. struct unity_map_entry *e);
  39. /* returns !0 if the IOMMU is caching non-present entries in its TLB */
  40. static int iommu_has_npcache(struct amd_iommu *iommu)
  41. {
  42. return iommu->cap & IOMMU_CAP_NPCACHE;
  43. }
  44. /****************************************************************************
  45. *
  46. * Interrupt handling functions
  47. *
  48. ****************************************************************************/
  49. static void iommu_print_event(void *__evt)
  50. {
  51. u32 *event = __evt;
  52. int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  53. int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
  54. int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
  55. int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
  56. u64 address = (u64)(((u64)event[3]) << 32) | event[2];
  57. printk(KERN_ERR "AMD IOMMU: Event logged [");
  58. switch (type) {
  59. case EVENT_TYPE_ILL_DEV:
  60. printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
  61. "address=0x%016llx flags=0x%04x]\n",
  62. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  63. address, flags);
  64. break;
  65. case EVENT_TYPE_IO_FAULT:
  66. printk("IO_PAGE_FAULT device=%02x:%02x.%x "
  67. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  68. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  69. domid, address, flags);
  70. break;
  71. case EVENT_TYPE_DEV_TAB_ERR:
  72. printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  73. "address=0x%016llx flags=0x%04x]\n",
  74. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  75. address, flags);
  76. break;
  77. case EVENT_TYPE_PAGE_TAB_ERR:
  78. printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  79. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  80. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  81. domid, address, flags);
  82. break;
  83. case EVENT_TYPE_ILL_CMD:
  84. printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
  85. break;
  86. case EVENT_TYPE_CMD_HARD_ERR:
  87. printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
  88. "flags=0x%04x]\n", address, flags);
  89. break;
  90. case EVENT_TYPE_IOTLB_INV_TO:
  91. printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
  92. "address=0x%016llx]\n",
  93. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  94. address);
  95. break;
  96. case EVENT_TYPE_INV_DEV_REQ:
  97. printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
  98. "address=0x%016llx flags=0x%04x]\n",
  99. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  100. address, flags);
  101. break;
  102. default:
  103. printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
  104. }
  105. }
  106. static void iommu_poll_events(struct amd_iommu *iommu)
  107. {
  108. u32 head, tail;
  109. unsigned long flags;
  110. spin_lock_irqsave(&iommu->lock, flags);
  111. head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  112. tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  113. while (head != tail) {
  114. iommu_print_event(iommu->evt_buf + head);
  115. head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
  116. }
  117. writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  118. spin_unlock_irqrestore(&iommu->lock, flags);
  119. }
  120. irqreturn_t amd_iommu_int_handler(int irq, void *data)
  121. {
  122. struct amd_iommu *iommu;
  123. list_for_each_entry(iommu, &amd_iommu_list, list)
  124. iommu_poll_events(iommu);
  125. return IRQ_HANDLED;
  126. }
  127. /****************************************************************************
  128. *
  129. * IOMMU command queuing functions
  130. *
  131. ****************************************************************************/
  132. /*
  133. * Writes the command to the IOMMUs command buffer and informs the
  134. * hardware about the new command. Must be called with iommu->lock held.
  135. */
  136. static int __iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  137. {
  138. u32 tail, head;
  139. u8 *target;
  140. tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  141. target = iommu->cmd_buf + tail;
  142. memcpy_toio(target, cmd, sizeof(*cmd));
  143. tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
  144. head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  145. if (tail == head)
  146. return -ENOMEM;
  147. writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  148. return 0;
  149. }
  150. /*
  151. * General queuing function for commands. Takes iommu->lock and calls
  152. * __iommu_queue_command().
  153. */
  154. static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  155. {
  156. unsigned long flags;
  157. int ret;
  158. spin_lock_irqsave(&iommu->lock, flags);
  159. ret = __iommu_queue_command(iommu, cmd);
  160. spin_unlock_irqrestore(&iommu->lock, flags);
  161. return ret;
  162. }
  163. /*
  164. * This function is called whenever we need to ensure that the IOMMU has
  165. * completed execution of all commands we sent. It sends a
  166. * COMPLETION_WAIT command and waits for it to finish. The IOMMU informs
  167. * us about that by writing a value to a physical address we pass with
  168. * the command.
  169. */
  170. static int iommu_completion_wait(struct amd_iommu *iommu)
  171. {
  172. int ret, ready = 0;
  173. unsigned status = 0;
  174. struct iommu_cmd cmd;
  175. unsigned long i = 0;
  176. memset(&cmd, 0, sizeof(cmd));
  177. cmd.data[0] = CMD_COMPL_WAIT_INT_MASK;
  178. CMD_SET_TYPE(&cmd, CMD_COMPL_WAIT);
  179. iommu->need_sync = 0;
  180. ret = iommu_queue_command(iommu, &cmd);
  181. if (ret)
  182. return ret;
  183. while (!ready && (i < EXIT_LOOP_COUNT)) {
  184. ++i;
  185. /* wait for the bit to become one */
  186. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  187. ready = status & MMIO_STATUS_COM_WAIT_INT_MASK;
  188. }
  189. /* set bit back to zero */
  190. status &= ~MMIO_STATUS_COM_WAIT_INT_MASK;
  191. writel(status, iommu->mmio_base + MMIO_STATUS_OFFSET);
  192. if (unlikely((i == EXIT_LOOP_COUNT) && printk_ratelimit()))
  193. printk(KERN_WARNING "AMD IOMMU: Completion wait loop failed\n");
  194. return 0;
  195. }
  196. /*
  197. * Command send function for invalidating a device table entry
  198. */
  199. static int iommu_queue_inv_dev_entry(struct amd_iommu *iommu, u16 devid)
  200. {
  201. struct iommu_cmd cmd;
  202. BUG_ON(iommu == NULL);
  203. memset(&cmd, 0, sizeof(cmd));
  204. CMD_SET_TYPE(&cmd, CMD_INV_DEV_ENTRY);
  205. cmd.data[0] = devid;
  206. iommu->need_sync = 1;
  207. return iommu_queue_command(iommu, &cmd);
  208. }
  209. /*
  210. * Generic command send function for invalidaing TLB entries
  211. */
  212. static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu,
  213. u64 address, u16 domid, int pde, int s)
  214. {
  215. struct iommu_cmd cmd;
  216. memset(&cmd, 0, sizeof(cmd));
  217. address &= PAGE_MASK;
  218. CMD_SET_TYPE(&cmd, CMD_INV_IOMMU_PAGES);
  219. cmd.data[1] |= domid;
  220. cmd.data[2] = lower_32_bits(address);
  221. cmd.data[3] = upper_32_bits(address);
  222. if (s) /* size bit - we flush more than one 4kb page */
  223. cmd.data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  224. if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
  225. cmd.data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  226. iommu->need_sync = 1;
  227. return iommu_queue_command(iommu, &cmd);
  228. }
  229. /*
  230. * TLB invalidation function which is called from the mapping functions.
  231. * It invalidates a single PTE if the range to flush is within a single
  232. * page. Otherwise it flushes the whole TLB of the IOMMU.
  233. */
  234. static int iommu_flush_pages(struct amd_iommu *iommu, u16 domid,
  235. u64 address, size_t size)
  236. {
  237. int s = 0;
  238. unsigned pages = iommu_num_pages(address, size);
  239. address &= PAGE_MASK;
  240. if (pages > 1) {
  241. /*
  242. * If we have to flush more than one page, flush all
  243. * TLB entries for this domain
  244. */
  245. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  246. s = 1;
  247. }
  248. iommu_queue_inv_iommu_pages(iommu, address, domid, 0, s);
  249. return 0;
  250. }
  251. /* Flush the whole IO/TLB for a given protection domain */
  252. static void iommu_flush_tlb(struct amd_iommu *iommu, u16 domid)
  253. {
  254. u64 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  255. iommu_queue_inv_iommu_pages(iommu, address, domid, 0, 1);
  256. }
  257. /****************************************************************************
  258. *
  259. * The functions below are used the create the page table mappings for
  260. * unity mapped regions.
  261. *
  262. ****************************************************************************/
  263. /*
  264. * Generic mapping functions. It maps a physical address into a DMA
  265. * address space. It allocates the page table pages if necessary.
  266. * In the future it can be extended to a generic mapping function
  267. * supporting all features of AMD IOMMU page tables like level skipping
  268. * and full 64 bit address spaces.
  269. */
  270. static int iommu_map(struct protection_domain *dom,
  271. unsigned long bus_addr,
  272. unsigned long phys_addr,
  273. int prot)
  274. {
  275. u64 __pte, *pte, *page;
  276. bus_addr = PAGE_ALIGN(bus_addr);
  277. phys_addr = PAGE_ALIGN(bus_addr);
  278. /* only support 512GB address spaces for now */
  279. if (bus_addr > IOMMU_MAP_SIZE_L3 || !(prot & IOMMU_PROT_MASK))
  280. return -EINVAL;
  281. pte = &dom->pt_root[IOMMU_PTE_L2_INDEX(bus_addr)];
  282. if (!IOMMU_PTE_PRESENT(*pte)) {
  283. page = (u64 *)get_zeroed_page(GFP_KERNEL);
  284. if (!page)
  285. return -ENOMEM;
  286. *pte = IOMMU_L2_PDE(virt_to_phys(page));
  287. }
  288. pte = IOMMU_PTE_PAGE(*pte);
  289. pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)];
  290. if (!IOMMU_PTE_PRESENT(*pte)) {
  291. page = (u64 *)get_zeroed_page(GFP_KERNEL);
  292. if (!page)
  293. return -ENOMEM;
  294. *pte = IOMMU_L1_PDE(virt_to_phys(page));
  295. }
  296. pte = IOMMU_PTE_PAGE(*pte);
  297. pte = &pte[IOMMU_PTE_L0_INDEX(bus_addr)];
  298. if (IOMMU_PTE_PRESENT(*pte))
  299. return -EBUSY;
  300. __pte = phys_addr | IOMMU_PTE_P;
  301. if (prot & IOMMU_PROT_IR)
  302. __pte |= IOMMU_PTE_IR;
  303. if (prot & IOMMU_PROT_IW)
  304. __pte |= IOMMU_PTE_IW;
  305. *pte = __pte;
  306. return 0;
  307. }
  308. /*
  309. * This function checks if a specific unity mapping entry is needed for
  310. * this specific IOMMU.
  311. */
  312. static int iommu_for_unity_map(struct amd_iommu *iommu,
  313. struct unity_map_entry *entry)
  314. {
  315. u16 bdf, i;
  316. for (i = entry->devid_start; i <= entry->devid_end; ++i) {
  317. bdf = amd_iommu_alias_table[i];
  318. if (amd_iommu_rlookup_table[bdf] == iommu)
  319. return 1;
  320. }
  321. return 0;
  322. }
  323. /*
  324. * Init the unity mappings for a specific IOMMU in the system
  325. *
  326. * Basically iterates over all unity mapping entries and applies them to
  327. * the default domain DMA of that IOMMU if necessary.
  328. */
  329. static int iommu_init_unity_mappings(struct amd_iommu *iommu)
  330. {
  331. struct unity_map_entry *entry;
  332. int ret;
  333. list_for_each_entry(entry, &amd_iommu_unity_map, list) {
  334. if (!iommu_for_unity_map(iommu, entry))
  335. continue;
  336. ret = dma_ops_unity_map(iommu->default_dom, entry);
  337. if (ret)
  338. return ret;
  339. }
  340. return 0;
  341. }
  342. /*
  343. * This function actually applies the mapping to the page table of the
  344. * dma_ops domain.
  345. */
  346. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  347. struct unity_map_entry *e)
  348. {
  349. u64 addr;
  350. int ret;
  351. for (addr = e->address_start; addr < e->address_end;
  352. addr += PAGE_SIZE) {
  353. ret = iommu_map(&dma_dom->domain, addr, addr, e->prot);
  354. if (ret)
  355. return ret;
  356. /*
  357. * if unity mapping is in aperture range mark the page
  358. * as allocated in the aperture
  359. */
  360. if (addr < dma_dom->aperture_size)
  361. __set_bit(addr >> PAGE_SHIFT, dma_dom->bitmap);
  362. }
  363. return 0;
  364. }
  365. /*
  366. * Inits the unity mappings required for a specific device
  367. */
  368. static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
  369. u16 devid)
  370. {
  371. struct unity_map_entry *e;
  372. int ret;
  373. list_for_each_entry(e, &amd_iommu_unity_map, list) {
  374. if (!(devid >= e->devid_start && devid <= e->devid_end))
  375. continue;
  376. ret = dma_ops_unity_map(dma_dom, e);
  377. if (ret)
  378. return ret;
  379. }
  380. return 0;
  381. }
  382. /****************************************************************************
  383. *
  384. * The next functions belong to the address allocator for the dma_ops
  385. * interface functions. They work like the allocators in the other IOMMU
  386. * drivers. Its basically a bitmap which marks the allocated pages in
  387. * the aperture. Maybe it could be enhanced in the future to a more
  388. * efficient allocator.
  389. *
  390. ****************************************************************************/
  391. static unsigned long dma_mask_to_pages(unsigned long mask)
  392. {
  393. return (mask >> PAGE_SHIFT) +
  394. (PAGE_ALIGN(mask & ~PAGE_MASK) >> PAGE_SHIFT);
  395. }
  396. /*
  397. * The address allocator core function.
  398. *
  399. * called with domain->lock held
  400. */
  401. static unsigned long dma_ops_alloc_addresses(struct device *dev,
  402. struct dma_ops_domain *dom,
  403. unsigned int pages,
  404. unsigned long align_mask)
  405. {
  406. unsigned long limit = dma_mask_to_pages(*dev->dma_mask);
  407. unsigned long address;
  408. unsigned long size = dom->aperture_size >> PAGE_SHIFT;
  409. unsigned long boundary_size;
  410. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  411. PAGE_SIZE) >> PAGE_SHIFT;
  412. limit = limit < size ? limit : size;
  413. if (dom->next_bit >= limit) {
  414. dom->next_bit = 0;
  415. dom->need_flush = true;
  416. }
  417. address = iommu_area_alloc(dom->bitmap, limit, dom->next_bit, pages,
  418. 0 , boundary_size, align_mask);
  419. if (address == -1) {
  420. address = iommu_area_alloc(dom->bitmap, limit, 0, pages,
  421. 0, boundary_size, align_mask);
  422. dom->need_flush = true;
  423. }
  424. if (likely(address != -1)) {
  425. dom->next_bit = address + pages;
  426. address <<= PAGE_SHIFT;
  427. } else
  428. address = bad_dma_address;
  429. WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
  430. return address;
  431. }
  432. /*
  433. * The address free function.
  434. *
  435. * called with domain->lock held
  436. */
  437. static void dma_ops_free_addresses(struct dma_ops_domain *dom,
  438. unsigned long address,
  439. unsigned int pages)
  440. {
  441. address >>= PAGE_SHIFT;
  442. iommu_area_free(dom->bitmap, address, pages);
  443. }
  444. /****************************************************************************
  445. *
  446. * The next functions belong to the domain allocation. A domain is
  447. * allocated for every IOMMU as the default domain. If device isolation
  448. * is enabled, every device get its own domain. The most important thing
  449. * about domains is the page table mapping the DMA address space they
  450. * contain.
  451. *
  452. ****************************************************************************/
  453. static u16 domain_id_alloc(void)
  454. {
  455. unsigned long flags;
  456. int id;
  457. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  458. id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
  459. BUG_ON(id == 0);
  460. if (id > 0 && id < MAX_DOMAIN_ID)
  461. __set_bit(id, amd_iommu_pd_alloc_bitmap);
  462. else
  463. id = 0;
  464. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  465. return id;
  466. }
  467. /*
  468. * Used to reserve address ranges in the aperture (e.g. for exclusion
  469. * ranges.
  470. */
  471. static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
  472. unsigned long start_page,
  473. unsigned int pages)
  474. {
  475. unsigned int last_page = dom->aperture_size >> PAGE_SHIFT;
  476. if (start_page + pages > last_page)
  477. pages = last_page - start_page;
  478. set_bit_string(dom->bitmap, start_page, pages);
  479. }
  480. static void dma_ops_free_pagetable(struct dma_ops_domain *dma_dom)
  481. {
  482. int i, j;
  483. u64 *p1, *p2, *p3;
  484. p1 = dma_dom->domain.pt_root;
  485. if (!p1)
  486. return;
  487. for (i = 0; i < 512; ++i) {
  488. if (!IOMMU_PTE_PRESENT(p1[i]))
  489. continue;
  490. p2 = IOMMU_PTE_PAGE(p1[i]);
  491. for (j = 0; j < 512; ++i) {
  492. if (!IOMMU_PTE_PRESENT(p2[j]))
  493. continue;
  494. p3 = IOMMU_PTE_PAGE(p2[j]);
  495. free_page((unsigned long)p3);
  496. }
  497. free_page((unsigned long)p2);
  498. }
  499. free_page((unsigned long)p1);
  500. }
  501. /*
  502. * Free a domain, only used if something went wrong in the
  503. * allocation path and we need to free an already allocated page table
  504. */
  505. static void dma_ops_domain_free(struct dma_ops_domain *dom)
  506. {
  507. if (!dom)
  508. return;
  509. dma_ops_free_pagetable(dom);
  510. kfree(dom->pte_pages);
  511. kfree(dom->bitmap);
  512. kfree(dom);
  513. }
  514. /*
  515. * Allocates a new protection domain usable for the dma_ops functions.
  516. * It also intializes the page table and the address allocator data
  517. * structures required for the dma_ops interface
  518. */
  519. static struct dma_ops_domain *dma_ops_domain_alloc(struct amd_iommu *iommu,
  520. unsigned order)
  521. {
  522. struct dma_ops_domain *dma_dom;
  523. unsigned i, num_pte_pages;
  524. u64 *l2_pde;
  525. u64 address;
  526. /*
  527. * Currently the DMA aperture must be between 32 MB and 1GB in size
  528. */
  529. if ((order < 25) || (order > 30))
  530. return NULL;
  531. dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
  532. if (!dma_dom)
  533. return NULL;
  534. spin_lock_init(&dma_dom->domain.lock);
  535. dma_dom->domain.id = domain_id_alloc();
  536. if (dma_dom->domain.id == 0)
  537. goto free_dma_dom;
  538. dma_dom->domain.mode = PAGE_MODE_3_LEVEL;
  539. dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  540. dma_dom->domain.priv = dma_dom;
  541. if (!dma_dom->domain.pt_root)
  542. goto free_dma_dom;
  543. dma_dom->aperture_size = (1ULL << order);
  544. dma_dom->bitmap = kzalloc(dma_dom->aperture_size / (PAGE_SIZE * 8),
  545. GFP_KERNEL);
  546. if (!dma_dom->bitmap)
  547. goto free_dma_dom;
  548. /*
  549. * mark the first page as allocated so we never return 0 as
  550. * a valid dma-address. So we can use 0 as error value
  551. */
  552. dma_dom->bitmap[0] = 1;
  553. dma_dom->next_bit = 0;
  554. dma_dom->need_flush = false;
  555. /* Intialize the exclusion range if necessary */
  556. if (iommu->exclusion_start &&
  557. iommu->exclusion_start < dma_dom->aperture_size) {
  558. unsigned long startpage = iommu->exclusion_start >> PAGE_SHIFT;
  559. int pages = iommu_num_pages(iommu->exclusion_start,
  560. iommu->exclusion_length);
  561. dma_ops_reserve_addresses(dma_dom, startpage, pages);
  562. }
  563. /*
  564. * At the last step, build the page tables so we don't need to
  565. * allocate page table pages in the dma_ops mapping/unmapping
  566. * path.
  567. */
  568. num_pte_pages = dma_dom->aperture_size / (PAGE_SIZE * 512);
  569. dma_dom->pte_pages = kzalloc(num_pte_pages * sizeof(void *),
  570. GFP_KERNEL);
  571. if (!dma_dom->pte_pages)
  572. goto free_dma_dom;
  573. l2_pde = (u64 *)get_zeroed_page(GFP_KERNEL);
  574. if (l2_pde == NULL)
  575. goto free_dma_dom;
  576. dma_dom->domain.pt_root[0] = IOMMU_L2_PDE(virt_to_phys(l2_pde));
  577. for (i = 0; i < num_pte_pages; ++i) {
  578. dma_dom->pte_pages[i] = (u64 *)get_zeroed_page(GFP_KERNEL);
  579. if (!dma_dom->pte_pages[i])
  580. goto free_dma_dom;
  581. address = virt_to_phys(dma_dom->pte_pages[i]);
  582. l2_pde[i] = IOMMU_L1_PDE(address);
  583. }
  584. return dma_dom;
  585. free_dma_dom:
  586. dma_ops_domain_free(dma_dom);
  587. return NULL;
  588. }
  589. /*
  590. * Find out the protection domain structure for a given PCI device. This
  591. * will give us the pointer to the page table root for example.
  592. */
  593. static struct protection_domain *domain_for_device(u16 devid)
  594. {
  595. struct protection_domain *dom;
  596. unsigned long flags;
  597. read_lock_irqsave(&amd_iommu_devtable_lock, flags);
  598. dom = amd_iommu_pd_table[devid];
  599. read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  600. return dom;
  601. }
  602. /*
  603. * If a device is not yet associated with a domain, this function does
  604. * assigns it visible for the hardware
  605. */
  606. static void set_device_domain(struct amd_iommu *iommu,
  607. struct protection_domain *domain,
  608. u16 devid)
  609. {
  610. unsigned long flags;
  611. u64 pte_root = virt_to_phys(domain->pt_root);
  612. pte_root |= (domain->mode & 0x07) << 9;
  613. pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | 2;
  614. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  615. amd_iommu_dev_table[devid].data[0] = pte_root;
  616. amd_iommu_dev_table[devid].data[1] = pte_root >> 32;
  617. amd_iommu_dev_table[devid].data[2] = domain->id;
  618. amd_iommu_pd_table[devid] = domain;
  619. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  620. iommu_queue_inv_dev_entry(iommu, devid);
  621. iommu->need_sync = 1;
  622. }
  623. /*****************************************************************************
  624. *
  625. * The next functions belong to the dma_ops mapping/unmapping code.
  626. *
  627. *****************************************************************************/
  628. /*
  629. * This function checks if the driver got a valid device from the caller to
  630. * avoid dereferencing invalid pointers.
  631. */
  632. static bool check_device(struct device *dev)
  633. {
  634. if (!dev || !dev->dma_mask)
  635. return false;
  636. return true;
  637. }
  638. /*
  639. * In the dma_ops path we only have the struct device. This function
  640. * finds the corresponding IOMMU, the protection domain and the
  641. * requestor id for a given device.
  642. * If the device is not yet associated with a domain this is also done
  643. * in this function.
  644. */
  645. static int get_device_resources(struct device *dev,
  646. struct amd_iommu **iommu,
  647. struct protection_domain **domain,
  648. u16 *bdf)
  649. {
  650. struct dma_ops_domain *dma_dom;
  651. struct pci_dev *pcidev;
  652. u16 _bdf;
  653. *iommu = NULL;
  654. *domain = NULL;
  655. *bdf = 0xffff;
  656. if (dev->bus != &pci_bus_type)
  657. return 0;
  658. pcidev = to_pci_dev(dev);
  659. _bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
  660. /* device not translated by any IOMMU in the system? */
  661. if (_bdf > amd_iommu_last_bdf)
  662. return 0;
  663. *bdf = amd_iommu_alias_table[_bdf];
  664. *iommu = amd_iommu_rlookup_table[*bdf];
  665. if (*iommu == NULL)
  666. return 0;
  667. dma_dom = (*iommu)->default_dom;
  668. *domain = domain_for_device(*bdf);
  669. if (*domain == NULL) {
  670. *domain = &dma_dom->domain;
  671. set_device_domain(*iommu, *domain, *bdf);
  672. printk(KERN_INFO "AMD IOMMU: Using protection domain %d for "
  673. "device ", (*domain)->id);
  674. print_devid(_bdf, 1);
  675. }
  676. return 1;
  677. }
  678. /*
  679. * This is the generic map function. It maps one 4kb page at paddr to
  680. * the given address in the DMA address space for the domain.
  681. */
  682. static dma_addr_t dma_ops_domain_map(struct amd_iommu *iommu,
  683. struct dma_ops_domain *dom,
  684. unsigned long address,
  685. phys_addr_t paddr,
  686. int direction)
  687. {
  688. u64 *pte, __pte;
  689. WARN_ON(address > dom->aperture_size);
  690. paddr &= PAGE_MASK;
  691. pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)];
  692. pte += IOMMU_PTE_L0_INDEX(address);
  693. __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
  694. if (direction == DMA_TO_DEVICE)
  695. __pte |= IOMMU_PTE_IR;
  696. else if (direction == DMA_FROM_DEVICE)
  697. __pte |= IOMMU_PTE_IW;
  698. else if (direction == DMA_BIDIRECTIONAL)
  699. __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
  700. WARN_ON(*pte);
  701. *pte = __pte;
  702. return (dma_addr_t)address;
  703. }
  704. /*
  705. * The generic unmapping function for on page in the DMA address space.
  706. */
  707. static void dma_ops_domain_unmap(struct amd_iommu *iommu,
  708. struct dma_ops_domain *dom,
  709. unsigned long address)
  710. {
  711. u64 *pte;
  712. if (address >= dom->aperture_size)
  713. return;
  714. WARN_ON(address & 0xfffULL || address > dom->aperture_size);
  715. pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)];
  716. pte += IOMMU_PTE_L0_INDEX(address);
  717. WARN_ON(!*pte);
  718. *pte = 0ULL;
  719. }
  720. /*
  721. * This function contains common code for mapping of a physically
  722. * contiguous memory region into DMA address space. It is uses by all
  723. * mapping functions provided by this IOMMU driver.
  724. * Must be called with the domain lock held.
  725. */
  726. static dma_addr_t __map_single(struct device *dev,
  727. struct amd_iommu *iommu,
  728. struct dma_ops_domain *dma_dom,
  729. phys_addr_t paddr,
  730. size_t size,
  731. int dir,
  732. bool align)
  733. {
  734. dma_addr_t offset = paddr & ~PAGE_MASK;
  735. dma_addr_t address, start;
  736. unsigned int pages;
  737. unsigned long align_mask = 0;
  738. int i;
  739. pages = iommu_num_pages(paddr, size);
  740. paddr &= PAGE_MASK;
  741. if (align)
  742. align_mask = (1UL << get_order(size)) - 1;
  743. address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask);
  744. if (unlikely(address == bad_dma_address))
  745. goto out;
  746. start = address;
  747. for (i = 0; i < pages; ++i) {
  748. dma_ops_domain_map(iommu, dma_dom, start, paddr, dir);
  749. paddr += PAGE_SIZE;
  750. start += PAGE_SIZE;
  751. }
  752. address += offset;
  753. if (unlikely(dma_dom->need_flush && !iommu_fullflush)) {
  754. iommu_flush_tlb(iommu, dma_dom->domain.id);
  755. dma_dom->need_flush = false;
  756. } else if (unlikely(iommu_has_npcache(iommu)))
  757. iommu_flush_pages(iommu, dma_dom->domain.id, address, size);
  758. out:
  759. return address;
  760. }
  761. /*
  762. * Does the reverse of the __map_single function. Must be called with
  763. * the domain lock held too
  764. */
  765. static void __unmap_single(struct amd_iommu *iommu,
  766. struct dma_ops_domain *dma_dom,
  767. dma_addr_t dma_addr,
  768. size_t size,
  769. int dir)
  770. {
  771. dma_addr_t i, start;
  772. unsigned int pages;
  773. if ((dma_addr == 0) || (dma_addr + size > dma_dom->aperture_size))
  774. return;
  775. pages = iommu_num_pages(dma_addr, size);
  776. dma_addr &= PAGE_MASK;
  777. start = dma_addr;
  778. for (i = 0; i < pages; ++i) {
  779. dma_ops_domain_unmap(iommu, dma_dom, start);
  780. start += PAGE_SIZE;
  781. }
  782. dma_ops_free_addresses(dma_dom, dma_addr, pages);
  783. if (iommu_fullflush)
  784. iommu_flush_pages(iommu, dma_dom->domain.id, dma_addr, size);
  785. }
  786. /*
  787. * The exported map_single function for dma_ops.
  788. */
  789. static dma_addr_t map_single(struct device *dev, phys_addr_t paddr,
  790. size_t size, int dir)
  791. {
  792. unsigned long flags;
  793. struct amd_iommu *iommu;
  794. struct protection_domain *domain;
  795. u16 devid;
  796. dma_addr_t addr;
  797. if (!check_device(dev))
  798. return bad_dma_address;
  799. get_device_resources(dev, &iommu, &domain, &devid);
  800. if (iommu == NULL || domain == NULL)
  801. /* device not handled by any AMD IOMMU */
  802. return (dma_addr_t)paddr;
  803. spin_lock_irqsave(&domain->lock, flags);
  804. addr = __map_single(dev, iommu, domain->priv, paddr, size, dir, false);
  805. if (addr == bad_dma_address)
  806. goto out;
  807. if (unlikely(iommu->need_sync))
  808. iommu_completion_wait(iommu);
  809. out:
  810. spin_unlock_irqrestore(&domain->lock, flags);
  811. return addr;
  812. }
  813. /*
  814. * The exported unmap_single function for dma_ops.
  815. */
  816. static void unmap_single(struct device *dev, dma_addr_t dma_addr,
  817. size_t size, int dir)
  818. {
  819. unsigned long flags;
  820. struct amd_iommu *iommu;
  821. struct protection_domain *domain;
  822. u16 devid;
  823. if (!check_device(dev) ||
  824. !get_device_resources(dev, &iommu, &domain, &devid))
  825. /* device not handled by any AMD IOMMU */
  826. return;
  827. spin_lock_irqsave(&domain->lock, flags);
  828. __unmap_single(iommu, domain->priv, dma_addr, size, dir);
  829. if (unlikely(iommu->need_sync))
  830. iommu_completion_wait(iommu);
  831. spin_unlock_irqrestore(&domain->lock, flags);
  832. }
  833. /*
  834. * This is a special map_sg function which is used if we should map a
  835. * device which is not handled by an AMD IOMMU in the system.
  836. */
  837. static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
  838. int nelems, int dir)
  839. {
  840. struct scatterlist *s;
  841. int i;
  842. for_each_sg(sglist, s, nelems, i) {
  843. s->dma_address = (dma_addr_t)sg_phys(s);
  844. s->dma_length = s->length;
  845. }
  846. return nelems;
  847. }
  848. /*
  849. * The exported map_sg function for dma_ops (handles scatter-gather
  850. * lists).
  851. */
  852. static int map_sg(struct device *dev, struct scatterlist *sglist,
  853. int nelems, int dir)
  854. {
  855. unsigned long flags;
  856. struct amd_iommu *iommu;
  857. struct protection_domain *domain;
  858. u16 devid;
  859. int i;
  860. struct scatterlist *s;
  861. phys_addr_t paddr;
  862. int mapped_elems = 0;
  863. if (!check_device(dev))
  864. return 0;
  865. get_device_resources(dev, &iommu, &domain, &devid);
  866. if (!iommu || !domain)
  867. return map_sg_no_iommu(dev, sglist, nelems, dir);
  868. spin_lock_irqsave(&domain->lock, flags);
  869. for_each_sg(sglist, s, nelems, i) {
  870. paddr = sg_phys(s);
  871. s->dma_address = __map_single(dev, iommu, domain->priv,
  872. paddr, s->length, dir, false);
  873. if (s->dma_address) {
  874. s->dma_length = s->length;
  875. mapped_elems++;
  876. } else
  877. goto unmap;
  878. }
  879. if (unlikely(iommu->need_sync))
  880. iommu_completion_wait(iommu);
  881. out:
  882. spin_unlock_irqrestore(&domain->lock, flags);
  883. return mapped_elems;
  884. unmap:
  885. for_each_sg(sglist, s, mapped_elems, i) {
  886. if (s->dma_address)
  887. __unmap_single(iommu, domain->priv, s->dma_address,
  888. s->dma_length, dir);
  889. s->dma_address = s->dma_length = 0;
  890. }
  891. mapped_elems = 0;
  892. goto out;
  893. }
  894. /*
  895. * The exported map_sg function for dma_ops (handles scatter-gather
  896. * lists).
  897. */
  898. static void unmap_sg(struct device *dev, struct scatterlist *sglist,
  899. int nelems, int dir)
  900. {
  901. unsigned long flags;
  902. struct amd_iommu *iommu;
  903. struct protection_domain *domain;
  904. struct scatterlist *s;
  905. u16 devid;
  906. int i;
  907. if (!check_device(dev) ||
  908. !get_device_resources(dev, &iommu, &domain, &devid))
  909. return;
  910. spin_lock_irqsave(&domain->lock, flags);
  911. for_each_sg(sglist, s, nelems, i) {
  912. __unmap_single(iommu, domain->priv, s->dma_address,
  913. s->dma_length, dir);
  914. s->dma_address = s->dma_length = 0;
  915. }
  916. if (unlikely(iommu->need_sync))
  917. iommu_completion_wait(iommu);
  918. spin_unlock_irqrestore(&domain->lock, flags);
  919. }
  920. /*
  921. * The exported alloc_coherent function for dma_ops.
  922. */
  923. static void *alloc_coherent(struct device *dev, size_t size,
  924. dma_addr_t *dma_addr, gfp_t flag)
  925. {
  926. unsigned long flags;
  927. void *virt_addr;
  928. struct amd_iommu *iommu;
  929. struct protection_domain *domain;
  930. u16 devid;
  931. phys_addr_t paddr;
  932. if (!check_device(dev))
  933. return NULL;
  934. virt_addr = (void *)__get_free_pages(flag, get_order(size));
  935. if (!virt_addr)
  936. return 0;
  937. memset(virt_addr, 0, size);
  938. paddr = virt_to_phys(virt_addr);
  939. get_device_resources(dev, &iommu, &domain, &devid);
  940. if (!iommu || !domain) {
  941. *dma_addr = (dma_addr_t)paddr;
  942. return virt_addr;
  943. }
  944. spin_lock_irqsave(&domain->lock, flags);
  945. *dma_addr = __map_single(dev, iommu, domain->priv, paddr,
  946. size, DMA_BIDIRECTIONAL, true);
  947. if (*dma_addr == bad_dma_address) {
  948. free_pages((unsigned long)virt_addr, get_order(size));
  949. virt_addr = NULL;
  950. goto out;
  951. }
  952. if (unlikely(iommu->need_sync))
  953. iommu_completion_wait(iommu);
  954. out:
  955. spin_unlock_irqrestore(&domain->lock, flags);
  956. return virt_addr;
  957. }
  958. /*
  959. * The exported free_coherent function for dma_ops.
  960. */
  961. static void free_coherent(struct device *dev, size_t size,
  962. void *virt_addr, dma_addr_t dma_addr)
  963. {
  964. unsigned long flags;
  965. struct amd_iommu *iommu;
  966. struct protection_domain *domain;
  967. u16 devid;
  968. if (!check_device(dev))
  969. return;
  970. get_device_resources(dev, &iommu, &domain, &devid);
  971. if (!iommu || !domain)
  972. goto free_mem;
  973. spin_lock_irqsave(&domain->lock, flags);
  974. __unmap_single(iommu, domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
  975. if (unlikely(iommu->need_sync))
  976. iommu_completion_wait(iommu);
  977. spin_unlock_irqrestore(&domain->lock, flags);
  978. free_mem:
  979. free_pages((unsigned long)virt_addr, get_order(size));
  980. }
  981. /*
  982. * This function is called by the DMA layer to find out if we can handle a
  983. * particular device. It is part of the dma_ops.
  984. */
  985. static int amd_iommu_dma_supported(struct device *dev, u64 mask)
  986. {
  987. u16 bdf;
  988. struct pci_dev *pcidev;
  989. /* No device or no PCI device */
  990. if (!dev || dev->bus != &pci_bus_type)
  991. return 0;
  992. pcidev = to_pci_dev(dev);
  993. bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
  994. /* Out of our scope? */
  995. if (bdf > amd_iommu_last_bdf)
  996. return 0;
  997. return 1;
  998. }
  999. /*
  1000. * The function for pre-allocating protection domains.
  1001. *
  1002. * If the driver core informs the DMA layer if a driver grabs a device
  1003. * we don't need to preallocate the protection domains anymore.
  1004. * For now we have to.
  1005. */
  1006. void prealloc_protection_domains(void)
  1007. {
  1008. struct pci_dev *dev = NULL;
  1009. struct dma_ops_domain *dma_dom;
  1010. struct amd_iommu *iommu;
  1011. int order = amd_iommu_aperture_order;
  1012. u16 devid;
  1013. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  1014. devid = (dev->bus->number << 8) | dev->devfn;
  1015. if (devid > amd_iommu_last_bdf)
  1016. continue;
  1017. devid = amd_iommu_alias_table[devid];
  1018. if (domain_for_device(devid))
  1019. continue;
  1020. iommu = amd_iommu_rlookup_table[devid];
  1021. if (!iommu)
  1022. continue;
  1023. dma_dom = dma_ops_domain_alloc(iommu, order);
  1024. if (!dma_dom)
  1025. continue;
  1026. init_unity_mappings_for_device(dma_dom, devid);
  1027. set_device_domain(iommu, &dma_dom->domain, devid);
  1028. printk(KERN_INFO "AMD IOMMU: Allocated domain %d for device ",
  1029. dma_dom->domain.id);
  1030. print_devid(devid, 1);
  1031. }
  1032. }
  1033. static struct dma_mapping_ops amd_iommu_dma_ops = {
  1034. .alloc_coherent = alloc_coherent,
  1035. .free_coherent = free_coherent,
  1036. .map_single = map_single,
  1037. .unmap_single = unmap_single,
  1038. .map_sg = map_sg,
  1039. .unmap_sg = unmap_sg,
  1040. .dma_supported = amd_iommu_dma_supported,
  1041. };
  1042. /*
  1043. * The function which clues the AMD IOMMU driver into dma_ops.
  1044. */
  1045. int __init amd_iommu_init_dma_ops(void)
  1046. {
  1047. struct amd_iommu *iommu;
  1048. int order = amd_iommu_aperture_order;
  1049. int ret;
  1050. /*
  1051. * first allocate a default protection domain for every IOMMU we
  1052. * found in the system. Devices not assigned to any other
  1053. * protection domain will be assigned to the default one.
  1054. */
  1055. list_for_each_entry(iommu, &amd_iommu_list, list) {
  1056. iommu->default_dom = dma_ops_domain_alloc(iommu, order);
  1057. if (iommu->default_dom == NULL)
  1058. return -ENOMEM;
  1059. ret = iommu_init_unity_mappings(iommu);
  1060. if (ret)
  1061. goto free_domains;
  1062. }
  1063. /*
  1064. * If device isolation is enabled, pre-allocate the protection
  1065. * domains for each device.
  1066. */
  1067. if (amd_iommu_isolate)
  1068. prealloc_protection_domains();
  1069. iommu_detected = 1;
  1070. force_iommu = 1;
  1071. bad_dma_address = 0;
  1072. #ifdef CONFIG_GART_IOMMU
  1073. gart_iommu_aperture_disabled = 1;
  1074. gart_iommu_aperture = 0;
  1075. #endif
  1076. /* Make the driver finally visible to the drivers */
  1077. dma_ops = &amd_iommu_dma_ops;
  1078. return 0;
  1079. free_domains:
  1080. list_for_each_entry(iommu, &amd_iommu_list, list) {
  1081. if (iommu->default_dom)
  1082. dma_ops_domain_free(iommu->default_dom);
  1083. }
  1084. return ret;
  1085. }