swiotlb.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Dynamic DMA mapping support.
  4. *
  5. * This implementation is a fallback for platforms that do not support
  6. * I/O TLBs (aka DMA address translation hardware).
  7. * Copyright (C) 2000 Asit Mallick <[email protected]>
  8. * Copyright (C) 2000 Goutham Rao <[email protected]>
  9. * Copyright (C) 2000, 2003 Hewlett-Packard Co
  10. * David Mosberger-Tang <[email protected]>
  11. *
  12. * 03/05/07 davidm Switch from PCI-DMA to generic device DMA API.
  13. * 00/12/13 davidm Rename to swiotlb.c and add mark_clean() to avoid
  14. * unnecessary i-cache flushing.
  15. * 04/07/.. ak Better overflow handling. Assorted fixes.
  16. * 05/09/10 linville Add support for syncing ranges, support syncing for
  17. * DMA_BIDIRECTIONAL mappings, miscellaneous cleanup.
  18. * 08/12/11 beckyb Add highmem support
  19. */
  20. #define pr_fmt(fmt) "software IO TLB: " fmt
  21. #include <linux/cache.h>
  22. #include <linux/cc_platform.h>
  23. #include <linux/ctype.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/dma-direct.h>
  26. #include <linux/dma-map-ops.h>
  27. #include <linux/export.h>
  28. #include <linux/gfp.h>
  29. #include <linux/highmem.h>
  30. #include <linux/io.h>
  31. #include <linux/iommu-helper.h>
  32. #include <linux/init.h>
  33. #include <linux/memblock.h>
  34. #include <linux/mm.h>
  35. #include <linux/pfn.h>
  36. #include <linux/scatterlist.h>
  37. #include <linux/set_memory.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/string.h>
  40. #include <linux/swiotlb.h>
  41. #include <linux/types.h>
  42. #ifdef CONFIG_DMA_RESTRICTED_POOL
  43. #include <linux/of.h>
  44. #include <linux/of_fdt.h>
  45. #include <linux/of_reserved_mem.h>
  46. #include <linux/slab.h>
  47. #endif
  48. #define CREATE_TRACE_POINTS
  49. #include <trace/events/swiotlb.h>
  50. #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
  51. /*
  52. * Minimum IO TLB size to bother booting with. Systems with mainly
  53. * 64bit capable cards will only lightly use the swiotlb. If we can't
  54. * allocate a contiguous 1MB, we're probably in trouble anyway.
  55. */
  56. #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
  57. #define INVALID_PHYS_ADDR (~(phys_addr_t)0)
  58. struct io_tlb_slot {
  59. phys_addr_t orig_addr;
  60. size_t alloc_size;
  61. unsigned int list;
  62. };
  63. static bool swiotlb_force_bounce;
  64. static bool swiotlb_force_disable;
  65. struct io_tlb_mem io_tlb_default_mem;
  66. phys_addr_t swiotlb_unencrypted_base;
  67. #ifdef CONFIG_SWIOTLB_NONLINEAR
  68. phys_addr_t io_tlb_start, io_tlb_end;
  69. static unsigned long io_tlb_nslabs;
  70. static char *io_tlb_vstart;
  71. static inline unsigned char *swiotlb_phys_to_virt(phys_addr_t tlb_addr);
  72. #else
  73. #define swiotlb_phys_to_virt phys_to_virt
  74. #endif
  75. static unsigned long default_nslabs = IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT;
  76. static unsigned long default_nareas;
  77. /**
  78. * struct io_tlb_area - IO TLB memory area descriptor
  79. *
  80. * This is a single area with a single lock.
  81. *
  82. * @used: The number of used IO TLB block.
  83. * @index: The slot index to start searching in this area for next round.
  84. * @lock: The lock to protect the above data structures in the map and
  85. * unmap calls.
  86. */
  87. struct io_tlb_area {
  88. unsigned long used;
  89. unsigned int index;
  90. spinlock_t lock;
  91. };
  92. /*
  93. * Round up number of slabs to the next power of 2. The last area is going
  94. * be smaller than the rest if default_nslabs is not power of two.
  95. * The number of slot in an area should be a multiple of IO_TLB_SEGSIZE,
  96. * otherwise a segment may span two or more areas. It conflicts with free
  97. * contiguous slots tracking: free slots are treated contiguous no matter
  98. * whether they cross an area boundary.
  99. *
  100. * Return true if default_nslabs is rounded up.
  101. */
  102. static bool round_up_default_nslabs(void)
  103. {
  104. if (!default_nareas)
  105. return false;
  106. if (default_nslabs < IO_TLB_SEGSIZE * default_nareas)
  107. default_nslabs = IO_TLB_SEGSIZE * default_nareas;
  108. else if (is_power_of_2(default_nslabs))
  109. return false;
  110. default_nslabs = roundup_pow_of_two(default_nslabs);
  111. return true;
  112. }
  113. /**
  114. * swiotlb_adjust_nareas() - adjust the number of areas and slots
  115. * @nareas: Desired number of areas. Zero is treated as 1.
  116. *
  117. * Adjust the default number of areas in a memory pool.
  118. * The default size of the memory pool may also change to meet minimum area
  119. * size requirements.
  120. */
  121. static void swiotlb_adjust_nareas(unsigned int nareas)
  122. {
  123. if (!nareas)
  124. nareas = 1;
  125. else if (!is_power_of_2(nareas))
  126. nareas = roundup_pow_of_two(nareas);
  127. default_nareas = nareas;
  128. pr_info("area num %d.\n", nareas);
  129. if (round_up_default_nslabs())
  130. pr_info("SWIOTLB bounce buffer size roundup to %luMB",
  131. (default_nslabs << IO_TLB_SHIFT) >> 20);
  132. }
  133. /**
  134. * limit_nareas() - get the maximum number of areas for a given memory pool size
  135. * @nareas: Desired number of areas.
  136. * @nslots: Total number of slots in the memory pool.
  137. *
  138. * Limit the number of areas to the maximum possible number of areas in
  139. * a memory pool of the given size.
  140. *
  141. * Return: Maximum possible number of areas.
  142. */
  143. static unsigned int limit_nareas(unsigned int nareas, unsigned long nslots)
  144. {
  145. if (nslots < nareas * IO_TLB_SEGSIZE)
  146. return nslots / IO_TLB_SEGSIZE;
  147. return nareas;
  148. }
  149. static int __init
  150. setup_io_tlb_npages(char *str)
  151. {
  152. if (isdigit(*str)) {
  153. /* avoid tail segment of size < IO_TLB_SEGSIZE */
  154. default_nslabs =
  155. ALIGN(simple_strtoul(str, &str, 0), IO_TLB_SEGSIZE);
  156. }
  157. if (*str == ',')
  158. ++str;
  159. if (isdigit(*str))
  160. swiotlb_adjust_nareas(simple_strtoul(str, &str, 0));
  161. if (*str == ',')
  162. ++str;
  163. if (!strcmp(str, "force"))
  164. swiotlb_force_bounce = true;
  165. else if (!strcmp(str, "noforce"))
  166. swiotlb_force_disable = true;
  167. return 0;
  168. }
  169. early_param("swiotlb", setup_io_tlb_npages);
  170. unsigned int swiotlb_max_segment(void)
  171. {
  172. if (!io_tlb_default_mem.nslabs)
  173. return 0;
  174. return rounddown(io_tlb_default_mem.nslabs << IO_TLB_SHIFT, PAGE_SIZE);
  175. }
  176. EXPORT_SYMBOL_GPL(swiotlb_max_segment);
  177. unsigned long swiotlb_size_or_default(void)
  178. {
  179. return default_nslabs << IO_TLB_SHIFT;
  180. }
  181. void __init swiotlb_adjust_size(unsigned long size)
  182. {
  183. /*
  184. * If swiotlb parameter has not been specified, give a chance to
  185. * architectures such as those supporting memory encryption to
  186. * adjust/expand SWIOTLB size for their use.
  187. */
  188. if (default_nslabs != IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT)
  189. return;
  190. size = ALIGN(size, IO_TLB_SIZE);
  191. default_nslabs = ALIGN(size >> IO_TLB_SHIFT, IO_TLB_SEGSIZE);
  192. if (round_up_default_nslabs())
  193. size = default_nslabs << IO_TLB_SHIFT;
  194. pr_info("SWIOTLB bounce buffer size adjusted to %luMB", size >> 20);
  195. }
  196. void swiotlb_print_info(void)
  197. {
  198. struct io_tlb_mem *mem = &io_tlb_default_mem;
  199. if (!mem->nslabs) {
  200. pr_warn("No low mem\n");
  201. return;
  202. }
  203. pr_info("mapped [mem %pa-%pa] (%luMB)\n", &mem->start, &mem->end,
  204. (mem->nslabs << IO_TLB_SHIFT) >> 20);
  205. }
  206. static inline unsigned long io_tlb_offset(unsigned long val)
  207. {
  208. return val & (IO_TLB_SEGSIZE - 1);
  209. }
  210. static inline unsigned long nr_slots(u64 val)
  211. {
  212. return DIV_ROUND_UP(val, IO_TLB_SIZE);
  213. }
  214. /*
  215. * Remap swioltb memory in the unencrypted physical address space
  216. * when swiotlb_unencrypted_base is set. (e.g. for Hyper-V AMD SEV-SNP
  217. * Isolation VMs).
  218. */
  219. #ifdef CONFIG_HAS_IOMEM
  220. static void *swiotlb_mem_remap(struct io_tlb_mem *mem, unsigned long bytes)
  221. {
  222. void *vaddr = NULL;
  223. if (swiotlb_unencrypted_base) {
  224. phys_addr_t paddr = mem->start + swiotlb_unencrypted_base;
  225. vaddr = memremap(paddr, bytes, MEMREMAP_WB);
  226. if (!vaddr)
  227. pr_err("Failed to map the unencrypted memory %pa size %lx.\n",
  228. &paddr, bytes);
  229. }
  230. return vaddr;
  231. }
  232. #else
  233. static void *swiotlb_mem_remap(struct io_tlb_mem *mem, unsigned long bytes)
  234. {
  235. return NULL;
  236. }
  237. #endif
  238. /*
  239. * Early SWIOTLB allocation may be too early to allow an architecture to
  240. * perform the desired operations. This function allows the architecture to
  241. * call SWIOTLB when the operations are possible. It needs to be called
  242. * before the SWIOTLB memory is used.
  243. */
  244. void __init swiotlb_update_mem_attributes(void)
  245. {
  246. struct io_tlb_mem *mem = &io_tlb_default_mem;
  247. void *vaddr;
  248. unsigned long bytes;
  249. if (!mem->nslabs || mem->late_alloc)
  250. return;
  251. vaddr = phys_to_virt(mem->start);
  252. bytes = PAGE_ALIGN(mem->nslabs << IO_TLB_SHIFT);
  253. set_memory_decrypted((unsigned long)vaddr, bytes >> PAGE_SHIFT);
  254. mem->vaddr = swiotlb_mem_remap(mem, bytes);
  255. if (!mem->vaddr)
  256. mem->vaddr = vaddr;
  257. }
  258. static void swiotlb_init_io_tlb_mem(struct io_tlb_mem *mem, phys_addr_t start,
  259. unsigned long nslabs, unsigned int flags,
  260. bool late_alloc, unsigned int nareas)
  261. {
  262. void *vaddr = swiotlb_phys_to_virt(start);
  263. unsigned long bytes = nslabs << IO_TLB_SHIFT, i;
  264. mem->nslabs = nslabs;
  265. mem->start = start;
  266. mem->end = mem->start + bytes;
  267. mem->late_alloc = late_alloc;
  268. mem->nareas = nareas;
  269. mem->area_nslabs = nslabs / mem->nareas;
  270. mem->force_bounce = swiotlb_force_bounce || (flags & SWIOTLB_FORCE);
  271. for (i = 0; i < mem->nareas; i++) {
  272. spin_lock_init(&mem->areas[i].lock);
  273. mem->areas[i].index = 0;
  274. mem->areas[i].used = 0;
  275. }
  276. for (i = 0; i < mem->nslabs; i++) {
  277. mem->slots[i].list = IO_TLB_SEGSIZE - io_tlb_offset(i);
  278. mem->slots[i].orig_addr = INVALID_PHYS_ADDR;
  279. mem->slots[i].alloc_size = 0;
  280. }
  281. /*
  282. * If swiotlb_unencrypted_base is set, the bounce buffer memory will
  283. * be remapped and cleared in swiotlb_update_mem_attributes.
  284. */
  285. if (swiotlb_unencrypted_base)
  286. return;
  287. memset(vaddr, 0, bytes);
  288. mem->vaddr = vaddr;
  289. return;
  290. }
  291. static void __init *swiotlb_memblock_alloc(unsigned long nslabs,
  292. unsigned int flags,
  293. int (*remap)(void *tlb, unsigned long nslabs))
  294. {
  295. size_t bytes = PAGE_ALIGN(nslabs << IO_TLB_SHIFT);
  296. void *tlb;
  297. /*
  298. * By default allocate the bounce buffer memory from low memory, but
  299. * allow to pick a location everywhere for hypervisors with guest
  300. * memory encryption.
  301. */
  302. if (flags & SWIOTLB_ANY)
  303. tlb = memblock_alloc(bytes, PAGE_SIZE);
  304. else
  305. tlb = memblock_alloc_low(bytes, PAGE_SIZE);
  306. if (!tlb) {
  307. pr_warn("%s: Failed to allocate %zu bytes tlb structure\n",
  308. __func__, bytes);
  309. return NULL;
  310. }
  311. if (remap && remap(tlb, nslabs) < 0) {
  312. memblock_free(tlb, PAGE_ALIGN(bytes));
  313. pr_warn("%s: Failed to remap %zu bytes\n", __func__, bytes);
  314. return NULL;
  315. }
  316. return tlb;
  317. }
  318. /*
  319. * Statically reserve bounce buffer space and initialize bounce buffer data
  320. * structures for the software IO TLB used to implement the DMA API.
  321. */
  322. void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags,
  323. int (*remap)(void *tlb, unsigned long nslabs))
  324. {
  325. struct io_tlb_mem *mem = &io_tlb_default_mem;
  326. unsigned long nslabs;
  327. unsigned int nareas;
  328. size_t alloc_size;
  329. void *tlb;
  330. if (!addressing_limit && !swiotlb_force_bounce)
  331. return;
  332. if (swiotlb_force_disable)
  333. return;
  334. if (!default_nareas)
  335. swiotlb_adjust_nareas(num_possible_cpus());
  336. nslabs = default_nslabs;
  337. nareas = limit_nareas(default_nareas, nslabs);
  338. while ((tlb = swiotlb_memblock_alloc(nslabs, flags, remap)) == NULL) {
  339. if (nslabs <= IO_TLB_MIN_SLABS)
  340. return;
  341. nslabs = ALIGN(nslabs >> 1, IO_TLB_SEGSIZE);
  342. nareas = limit_nareas(nareas, nslabs);
  343. }
  344. if (default_nslabs != nslabs) {
  345. pr_info("SWIOTLB bounce buffer size adjusted %lu -> %lu slabs",
  346. default_nslabs, nslabs);
  347. default_nslabs = nslabs;
  348. }
  349. alloc_size = PAGE_ALIGN(array_size(sizeof(*mem->slots), nslabs));
  350. mem->slots = memblock_alloc(alloc_size, PAGE_SIZE);
  351. if (!mem->slots) {
  352. pr_warn("%s: Failed to allocate %zu bytes align=0x%lx\n",
  353. __func__, alloc_size, PAGE_SIZE);
  354. return;
  355. }
  356. mem->areas = memblock_alloc(array_size(sizeof(struct io_tlb_area),
  357. default_nareas), SMP_CACHE_BYTES);
  358. if (!mem->areas) {
  359. pr_warn("%s: Failed to allocate mem->areas.\n", __func__);
  360. return;
  361. }
  362. swiotlb_init_io_tlb_mem(mem, __pa(tlb), nslabs, flags, false,
  363. default_nareas);
  364. if (flags & SWIOTLB_VERBOSE)
  365. swiotlb_print_info();
  366. }
  367. void __init swiotlb_init(bool addressing_limit, unsigned int flags)
  368. {
  369. swiotlb_init_remap(addressing_limit, flags, NULL);
  370. }
  371. /*
  372. * Systems with larger DMA zones (those that don't support ISA) can
  373. * initialize the swiotlb later using the slab allocator if needed.
  374. * This should be just like above, but with some error catching.
  375. */
  376. int swiotlb_init_late(size_t size, gfp_t gfp_mask,
  377. int (*remap)(void *tlb, unsigned long nslabs))
  378. {
  379. struct io_tlb_mem *mem = &io_tlb_default_mem;
  380. unsigned long nslabs = ALIGN(size >> IO_TLB_SHIFT, IO_TLB_SEGSIZE);
  381. unsigned int nareas;
  382. unsigned char *vstart = NULL;
  383. unsigned int order, area_order;
  384. bool retried = false;
  385. int rc = 0;
  386. if (swiotlb_force_disable)
  387. return 0;
  388. if (!default_nareas)
  389. swiotlb_adjust_nareas(num_possible_cpus());
  390. retry:
  391. order = get_order(nslabs << IO_TLB_SHIFT);
  392. nslabs = SLABS_PER_PAGE << order;
  393. while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
  394. vstart = (void *)__get_free_pages(gfp_mask | __GFP_NOWARN,
  395. order);
  396. if (vstart)
  397. break;
  398. order--;
  399. nslabs = SLABS_PER_PAGE << order;
  400. retried = true;
  401. }
  402. if (!vstart)
  403. return -ENOMEM;
  404. if (remap)
  405. rc = remap(vstart, nslabs);
  406. if (rc) {
  407. free_pages((unsigned long)vstart, order);
  408. nslabs = ALIGN(nslabs >> 1, IO_TLB_SEGSIZE);
  409. if (nslabs < IO_TLB_MIN_SLABS)
  410. return rc;
  411. retried = true;
  412. goto retry;
  413. }
  414. if (retried) {
  415. pr_warn("only able to allocate %ld MB\n",
  416. (PAGE_SIZE << order) >> 20);
  417. }
  418. nareas = limit_nareas(default_nareas, nslabs);
  419. area_order = get_order(array_size(sizeof(*mem->areas), nareas));
  420. mem->areas = (struct io_tlb_area *)
  421. __get_free_pages(GFP_KERNEL | __GFP_ZERO, area_order);
  422. if (!mem->areas)
  423. goto error_area;
  424. mem->slots = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  425. get_order(array_size(sizeof(*mem->slots), nslabs)));
  426. if (!mem->slots)
  427. goto error_slots;
  428. set_memory_decrypted((unsigned long)vstart,
  429. (nslabs << IO_TLB_SHIFT) >> PAGE_SHIFT);
  430. swiotlb_init_io_tlb_mem(mem, virt_to_phys(vstart), nslabs, 0, true,
  431. nareas);
  432. swiotlb_print_info();
  433. return 0;
  434. error_slots:
  435. free_pages((unsigned long)mem->areas, area_order);
  436. error_area:
  437. free_pages((unsigned long)vstart, order);
  438. return -ENOMEM;
  439. }
  440. #ifdef CONFIG_SWIOTLB_NONLINEAR
  441. static int swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs)
  442. {
  443. struct io_tlb_mem *mem = &io_tlb_default_mem;
  444. unsigned long bytes = nslabs << IO_TLB_SHIFT;
  445. unsigned int area_order;
  446. /* protect against double initialization */
  447. if (WARN_ON_ONCE(mem->nslabs))
  448. return -ENOMEM;
  449. if (!default_nareas)
  450. swiotlb_adjust_nareas(num_possible_cpus());
  451. area_order = get_order(array_size(sizeof(*mem->areas),
  452. default_nareas));
  453. mem->areas = (struct io_tlb_area *)
  454. __get_free_pages(GFP_KERNEL | __GFP_ZERO, area_order);
  455. if (!mem->areas)
  456. return -ENOMEM;
  457. mem->slots = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  458. get_order(array_size(sizeof(*mem->slots), nslabs)));
  459. if (!mem->slots)
  460. goto error_slots;
  461. set_memory_decrypted((unsigned long)tlb, bytes >> PAGE_SHIFT);
  462. swiotlb_init_io_tlb_mem(mem, io_tlb_start, nslabs, 0, true, default_nareas);
  463. swiotlb_print_info();
  464. return 0;
  465. error_slots:
  466. free_pages((unsigned long)mem->areas, area_order);
  467. return -ENOMEM;
  468. }
  469. int swiotlb_late_init_with_tblpaddr(char *tlb,
  470. phys_addr_t tlb_paddr, unsigned long nslabs)
  471. {
  472. unsigned long bytes;
  473. if (io_tlb_start)
  474. return -EBUSY;
  475. bytes = nslabs << IO_TLB_SHIFT;
  476. io_tlb_nslabs = nslabs;
  477. io_tlb_start = tlb_paddr;
  478. io_tlb_vstart = tlb;
  479. io_tlb_end = io_tlb_start + bytes;
  480. return swiotlb_late_init_with_tbl(tlb, nslabs);
  481. }
  482. EXPORT_SYMBOL(swiotlb_late_init_with_tblpaddr);
  483. #endif /* CONFIG_SWIOTLB_NONLINEAR */
  484. void __init swiotlb_exit(void)
  485. {
  486. struct io_tlb_mem *mem = &io_tlb_default_mem;
  487. unsigned long tbl_vaddr;
  488. size_t tbl_size, slots_size;
  489. unsigned int area_order;
  490. if (swiotlb_force_bounce)
  491. return;
  492. if (!mem->nslabs)
  493. return;
  494. pr_info("tearing down default memory pool\n");
  495. tbl_vaddr = (unsigned long)phys_to_virt(mem->start);
  496. tbl_size = PAGE_ALIGN(mem->end - mem->start);
  497. slots_size = PAGE_ALIGN(array_size(sizeof(*mem->slots), mem->nslabs));
  498. set_memory_encrypted(tbl_vaddr, tbl_size >> PAGE_SHIFT);
  499. if (mem->late_alloc) {
  500. area_order = get_order(array_size(sizeof(*mem->areas),
  501. mem->nareas));
  502. free_pages((unsigned long)mem->areas, area_order);
  503. free_pages(tbl_vaddr, get_order(tbl_size));
  504. free_pages((unsigned long)mem->slots, get_order(slots_size));
  505. } else {
  506. memblock_free_late(__pa(mem->areas),
  507. array_size(sizeof(*mem->areas), mem->nareas));
  508. memblock_free_late(mem->start, tbl_size);
  509. memblock_free_late(__pa(mem->slots), slots_size);
  510. }
  511. memset(mem, 0, sizeof(*mem));
  512. }
  513. /*
  514. * Return the offset into a iotlb slot required to keep the device happy.
  515. */
  516. static unsigned int swiotlb_align_offset(struct device *dev, u64 addr)
  517. {
  518. return addr & dma_get_min_align_mask(dev) & (IO_TLB_SIZE - 1);
  519. }
  520. #ifdef CONFIG_SWIOTLB_NONLINEAR
  521. static inline unsigned char *swiotlb_phys_to_virt(phys_addr_t tlb_addr)
  522. {
  523. return (unsigned char *)(io_tlb_vstart + (tlb_addr - io_tlb_start));
  524. }
  525. #endif
  526. /*
  527. * Bounce: copy the swiotlb buffer from or back to the original dma location
  528. */
  529. static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size,
  530. enum dma_data_direction dir)
  531. {
  532. struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
  533. int index = (tlb_addr - mem->start) >> IO_TLB_SHIFT;
  534. phys_addr_t orig_addr = mem->slots[index].orig_addr;
  535. size_t alloc_size = mem->slots[index].alloc_size;
  536. unsigned long pfn = PFN_DOWN(orig_addr);
  537. unsigned char *vaddr = mem->vaddr + tlb_addr - mem->start;
  538. unsigned int tlb_offset, orig_addr_offset;
  539. if (orig_addr == INVALID_PHYS_ADDR)
  540. return;
  541. tlb_offset = tlb_addr & (IO_TLB_SIZE - 1);
  542. orig_addr_offset = swiotlb_align_offset(dev, orig_addr);
  543. if (tlb_offset < orig_addr_offset) {
  544. dev_WARN_ONCE(dev, 1,
  545. "Access before mapping start detected. orig offset %u, requested offset %u.\n",
  546. orig_addr_offset, tlb_offset);
  547. return;
  548. }
  549. tlb_offset -= orig_addr_offset;
  550. if (tlb_offset > alloc_size) {
  551. dev_WARN_ONCE(dev, 1,
  552. "Buffer overflow detected. Allocation size: %zu. Mapping size: %zu+%u.\n",
  553. alloc_size, size, tlb_offset);
  554. return;
  555. }
  556. orig_addr += tlb_offset;
  557. alloc_size -= tlb_offset;
  558. if (size > alloc_size) {
  559. dev_WARN_ONCE(dev, 1,
  560. "Buffer overflow detected. Allocation size: %zu. Mapping size: %zu.\n",
  561. alloc_size, size);
  562. size = alloc_size;
  563. }
  564. if (PageHighMem(pfn_to_page(pfn))) {
  565. unsigned int offset = orig_addr & ~PAGE_MASK;
  566. struct page *page;
  567. unsigned int sz = 0;
  568. unsigned long flags;
  569. while (size) {
  570. sz = min_t(size_t, PAGE_SIZE - offset, size);
  571. local_irq_save(flags);
  572. page = pfn_to_page(pfn);
  573. if (dir == DMA_TO_DEVICE)
  574. memcpy_from_page(vaddr, page, offset, sz);
  575. else
  576. memcpy_to_page(page, offset, vaddr, sz);
  577. local_irq_restore(flags);
  578. size -= sz;
  579. pfn++;
  580. vaddr += sz;
  581. offset = 0;
  582. }
  583. } else if (dir == DMA_TO_DEVICE) {
  584. memcpy(vaddr, phys_to_virt(orig_addr), size);
  585. } else {
  586. memcpy(phys_to_virt(orig_addr), vaddr, size);
  587. }
  588. }
  589. static inline phys_addr_t slot_addr(phys_addr_t start, phys_addr_t idx)
  590. {
  591. return start + (idx << IO_TLB_SHIFT);
  592. }
  593. /*
  594. * Carefully handle integer overflow which can occur when boundary_mask == ~0UL.
  595. */
  596. static inline unsigned long get_max_slots(unsigned long boundary_mask)
  597. {
  598. if (boundary_mask == ~0UL)
  599. return 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
  600. return nr_slots(boundary_mask + 1);
  601. }
  602. static unsigned int wrap_area_index(struct io_tlb_mem *mem, unsigned int index)
  603. {
  604. if (index >= mem->area_nslabs)
  605. return 0;
  606. return index;
  607. }
  608. /*
  609. * Find a suitable number of IO TLB entries size that will fit this request and
  610. * allocate a buffer from that IO TLB pool.
  611. */
  612. static int swiotlb_do_find_slots(struct device *dev, int area_index,
  613. phys_addr_t orig_addr, size_t alloc_size,
  614. unsigned int alloc_align_mask)
  615. {
  616. struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
  617. struct io_tlb_area *area = mem->areas + area_index;
  618. unsigned long boundary_mask = dma_get_seg_boundary(dev);
  619. dma_addr_t tbl_dma_addr =
  620. phys_to_dma_unencrypted(dev, mem->start) & boundary_mask;
  621. unsigned long max_slots = get_max_slots(boundary_mask);
  622. unsigned int iotlb_align_mask =
  623. dma_get_min_align_mask(dev) & ~(IO_TLB_SIZE - 1);
  624. unsigned int nslots = nr_slots(alloc_size), stride;
  625. unsigned int index, wrap, count = 0, i;
  626. unsigned int offset = swiotlb_align_offset(dev, orig_addr);
  627. unsigned long flags;
  628. unsigned int slot_base;
  629. unsigned int slot_index;
  630. BUG_ON(!nslots);
  631. BUG_ON(area_index >= mem->nareas);
  632. /*
  633. * For mappings with an alignment requirement don't bother looping to
  634. * unaligned slots once we found an aligned one. For allocations of
  635. * PAGE_SIZE or larger only look for page aligned allocations.
  636. */
  637. stride = (iotlb_align_mask >> IO_TLB_SHIFT) + 1;
  638. if (alloc_size >= PAGE_SIZE)
  639. stride = max(stride, stride << (PAGE_SHIFT - IO_TLB_SHIFT));
  640. stride = max(stride, (alloc_align_mask >> IO_TLB_SHIFT) + 1);
  641. spin_lock_irqsave(&area->lock, flags);
  642. if (unlikely(nslots > mem->area_nslabs - area->used))
  643. goto not_found;
  644. slot_base = area_index * mem->area_nslabs;
  645. index = wrap = wrap_area_index(mem, ALIGN(area->index, stride));
  646. do {
  647. slot_index = slot_base + index;
  648. if (orig_addr &&
  649. (slot_addr(tbl_dma_addr, slot_index) &
  650. iotlb_align_mask) != (orig_addr & iotlb_align_mask)) {
  651. index = wrap_area_index(mem, index + 1);
  652. continue;
  653. }
  654. /*
  655. * If we find a slot that indicates we have 'nslots' number of
  656. * contiguous buffers, we allocate the buffers from that slot
  657. * and mark the entries as '0' indicating unavailable.
  658. */
  659. if (!iommu_is_span_boundary(slot_index, nslots,
  660. nr_slots(tbl_dma_addr),
  661. max_slots)) {
  662. if (mem->slots[slot_index].list >= nslots)
  663. goto found;
  664. }
  665. index = wrap_area_index(mem, index + stride);
  666. } while (index != wrap);
  667. not_found:
  668. spin_unlock_irqrestore(&area->lock, flags);
  669. return -1;
  670. found:
  671. for (i = slot_index; i < slot_index + nslots; i++) {
  672. mem->slots[i].list = 0;
  673. mem->slots[i].alloc_size = alloc_size - (offset +
  674. ((i - slot_index) << IO_TLB_SHIFT));
  675. }
  676. for (i = slot_index - 1;
  677. io_tlb_offset(i) != IO_TLB_SEGSIZE - 1 &&
  678. mem->slots[i].list; i--)
  679. mem->slots[i].list = ++count;
  680. /*
  681. * Update the indices to avoid searching in the next round.
  682. */
  683. if (index + nslots < mem->area_nslabs)
  684. area->index = index + nslots;
  685. else
  686. area->index = 0;
  687. area->used += nslots;
  688. spin_unlock_irqrestore(&area->lock, flags);
  689. return slot_index;
  690. }
  691. static int swiotlb_find_slots(struct device *dev, phys_addr_t orig_addr,
  692. size_t alloc_size, unsigned int alloc_align_mask)
  693. {
  694. struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
  695. int start = raw_smp_processor_id() & (mem->nareas - 1);
  696. int i = start, index;
  697. do {
  698. index = swiotlb_do_find_slots(dev, i, orig_addr, alloc_size,
  699. alloc_align_mask);
  700. if (index >= 0)
  701. return index;
  702. if (++i >= mem->nareas)
  703. i = 0;
  704. } while (i != start);
  705. return -1;
  706. }
  707. static unsigned long mem_used(struct io_tlb_mem *mem)
  708. {
  709. int i;
  710. unsigned long used = 0;
  711. for (i = 0; i < mem->nareas; i++)
  712. used += mem->areas[i].used;
  713. return used;
  714. }
  715. phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr,
  716. size_t mapping_size, size_t alloc_size,
  717. unsigned int alloc_align_mask, enum dma_data_direction dir,
  718. unsigned long attrs)
  719. {
  720. struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
  721. unsigned int offset = swiotlb_align_offset(dev, orig_addr);
  722. unsigned int i;
  723. int index;
  724. phys_addr_t tlb_addr;
  725. if (!mem || !mem->nslabs) {
  726. dev_warn_ratelimited(dev,
  727. "Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer");
  728. return (phys_addr_t)DMA_MAPPING_ERROR;
  729. }
  730. if (cc_platform_has(CC_ATTR_MEM_ENCRYPT))
  731. pr_warn_once("Memory encryption is active and system is using DMA bounce buffers\n");
  732. if (mapping_size > alloc_size) {
  733. dev_warn_once(dev, "Invalid sizes (mapping: %zd bytes, alloc: %zd bytes)",
  734. mapping_size, alloc_size);
  735. return (phys_addr_t)DMA_MAPPING_ERROR;
  736. }
  737. index = swiotlb_find_slots(dev, orig_addr,
  738. alloc_size + offset, alloc_align_mask);
  739. if (index == -1) {
  740. if (!(attrs & DMA_ATTR_NO_WARN))
  741. dev_warn_ratelimited(dev,
  742. "swiotlb buffer is full (sz: %zd bytes), total %lu (slots), used %lu (slots)\n",
  743. alloc_size, mem->nslabs, mem_used(mem));
  744. return (phys_addr_t)DMA_MAPPING_ERROR;
  745. }
  746. /*
  747. * Save away the mapping from the original address to the DMA address.
  748. * This is needed when we sync the memory. Then we sync the buffer if
  749. * needed.
  750. */
  751. for (i = 0; i < nr_slots(alloc_size + offset); i++)
  752. mem->slots[index + i].orig_addr = slot_addr(orig_addr, i);
  753. tlb_addr = slot_addr(mem->start, index) + offset;
  754. /*
  755. * When dir == DMA_FROM_DEVICE we could omit the copy from the orig
  756. * to the tlb buffer, if we knew for sure the device will
  757. * overwrite the entire current content. But we don't. Thus
  758. * unconditional bounce may prevent leaking swiotlb content (i.e.
  759. * kernel memory) to user-space.
  760. */
  761. swiotlb_bounce(dev, tlb_addr, mapping_size, DMA_TO_DEVICE);
  762. return tlb_addr;
  763. }
  764. static void swiotlb_release_slots(struct device *dev, phys_addr_t tlb_addr)
  765. {
  766. struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
  767. unsigned long flags;
  768. unsigned int offset = swiotlb_align_offset(dev, tlb_addr);
  769. int index = (tlb_addr - offset - mem->start) >> IO_TLB_SHIFT;
  770. int nslots = nr_slots(mem->slots[index].alloc_size + offset);
  771. int aindex = index / mem->area_nslabs;
  772. struct io_tlb_area *area = &mem->areas[aindex];
  773. int count, i;
  774. /*
  775. * Return the buffer to the free list by setting the corresponding
  776. * entries to indicate the number of contiguous entries available.
  777. * While returning the entries to the free list, we merge the entries
  778. * with slots below and above the pool being returned.
  779. */
  780. BUG_ON(aindex >= mem->nareas);
  781. spin_lock_irqsave(&area->lock, flags);
  782. if (index + nslots < ALIGN(index + 1, IO_TLB_SEGSIZE))
  783. count = mem->slots[index + nslots].list;
  784. else
  785. count = 0;
  786. /*
  787. * Step 1: return the slots to the free list, merging the slots with
  788. * superceeding slots
  789. */
  790. for (i = index + nslots - 1; i >= index; i--) {
  791. mem->slots[i].list = ++count;
  792. mem->slots[i].orig_addr = INVALID_PHYS_ADDR;
  793. mem->slots[i].alloc_size = 0;
  794. }
  795. /*
  796. * Step 2: merge the returned slots with the preceding slots, if
  797. * available (non zero)
  798. */
  799. for (i = index - 1;
  800. io_tlb_offset(i) != IO_TLB_SEGSIZE - 1 && mem->slots[i].list;
  801. i--)
  802. mem->slots[i].list = ++count;
  803. area->used -= nslots;
  804. spin_unlock_irqrestore(&area->lock, flags);
  805. }
  806. /*
  807. * tlb_addr is the physical address of the bounce buffer to unmap.
  808. */
  809. void swiotlb_tbl_unmap_single(struct device *dev, phys_addr_t tlb_addr,
  810. size_t mapping_size, enum dma_data_direction dir,
  811. unsigned long attrs)
  812. {
  813. /*
  814. * First, sync the memory before unmapping the entry
  815. */
  816. if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
  817. (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
  818. swiotlb_bounce(dev, tlb_addr, mapping_size, DMA_FROM_DEVICE);
  819. swiotlb_release_slots(dev, tlb_addr);
  820. }
  821. #ifdef CONFIG_SWIOTLB_NONLINEAR
  822. EXPORT_SYMBOL(swiotlb_tbl_unmap_single);
  823. #endif
  824. void swiotlb_sync_single_for_device(struct device *dev, phys_addr_t tlb_addr,
  825. size_t size, enum dma_data_direction dir)
  826. {
  827. if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
  828. swiotlb_bounce(dev, tlb_addr, size, DMA_TO_DEVICE);
  829. else
  830. BUG_ON(dir != DMA_FROM_DEVICE);
  831. }
  832. void swiotlb_sync_single_for_cpu(struct device *dev, phys_addr_t tlb_addr,
  833. size_t size, enum dma_data_direction dir)
  834. {
  835. if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
  836. swiotlb_bounce(dev, tlb_addr, size, DMA_FROM_DEVICE);
  837. else
  838. BUG_ON(dir != DMA_TO_DEVICE);
  839. }
  840. /*
  841. * Create a swiotlb mapping for the buffer at @paddr, and in case of DMAing
  842. * to the device copy the data into it as well.
  843. */
  844. dma_addr_t swiotlb_map(struct device *dev, phys_addr_t paddr, size_t size,
  845. enum dma_data_direction dir, unsigned long attrs)
  846. {
  847. phys_addr_t swiotlb_addr;
  848. dma_addr_t dma_addr;
  849. trace_swiotlb_bounced(dev, phys_to_dma(dev, paddr), size);
  850. swiotlb_addr = swiotlb_tbl_map_single(dev, paddr, size, size, 0, dir,
  851. attrs);
  852. if (swiotlb_addr == (phys_addr_t)DMA_MAPPING_ERROR)
  853. return DMA_MAPPING_ERROR;
  854. /* Ensure that the address returned is DMA'ble */
  855. dma_addr = phys_to_dma_unencrypted(dev, swiotlb_addr);
  856. if (unlikely(!dma_capable(dev, dma_addr, size, true))) {
  857. swiotlb_tbl_unmap_single(dev, swiotlb_addr, size, dir,
  858. attrs | DMA_ATTR_SKIP_CPU_SYNC);
  859. dev_WARN_ONCE(dev, 1,
  860. "swiotlb addr %pad+%zu overflow (mask %llx, bus limit %llx).\n",
  861. &dma_addr, size, *dev->dma_mask, dev->bus_dma_limit);
  862. return DMA_MAPPING_ERROR;
  863. }
  864. if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
  865. arch_sync_dma_for_device(swiotlb_addr, size, dir);
  866. return dma_addr;
  867. }
  868. #ifdef CONFIG_SWIOTLB_NONLINEAR
  869. EXPORT_SYMBOL(swiotlb_map);
  870. size_t swiotlb_max_mapping_size(struct device *dev)
  871. {
  872. return 4096;
  873. }
  874. #else
  875. size_t swiotlb_max_mapping_size(struct device *dev)
  876. {
  877. int min_align_mask = dma_get_min_align_mask(dev);
  878. int min_align = 0;
  879. /*
  880. * swiotlb_find_slots() skips slots according to
  881. * min align mask. This affects max mapping size.
  882. * Take it into acount here.
  883. */
  884. if (min_align_mask)
  885. min_align = roundup(min_align_mask, IO_TLB_SIZE);
  886. return ((size_t)IO_TLB_SIZE) * IO_TLB_SEGSIZE - min_align;
  887. }
  888. #endif
  889. bool is_swiotlb_active(struct device *dev)
  890. {
  891. struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
  892. return mem && mem->nslabs;
  893. }
  894. EXPORT_SYMBOL_GPL(is_swiotlb_active);
  895. static int io_tlb_used_get(void *data, u64 *val)
  896. {
  897. struct io_tlb_mem *mem = data;
  898. *val = mem_used(mem);
  899. return 0;
  900. }
  901. DEFINE_DEBUGFS_ATTRIBUTE(fops_io_tlb_used, io_tlb_used_get, NULL, "%llu\n");
  902. static void swiotlb_create_debugfs_files(struct io_tlb_mem *mem,
  903. const char *dirname)
  904. {
  905. mem->debugfs = debugfs_create_dir(dirname, io_tlb_default_mem.debugfs);
  906. if (!mem->nslabs)
  907. return;
  908. debugfs_create_ulong("io_tlb_nslabs", 0400, mem->debugfs, &mem->nslabs);
  909. debugfs_create_file("io_tlb_used", 0400, mem->debugfs, mem,
  910. &fops_io_tlb_used);
  911. }
  912. static int __init __maybe_unused swiotlb_create_default_debugfs(void)
  913. {
  914. swiotlb_create_debugfs_files(&io_tlb_default_mem, "swiotlb");
  915. return 0;
  916. }
  917. #ifdef CONFIG_DEBUG_FS
  918. late_initcall(swiotlb_create_default_debugfs);
  919. #endif
  920. #ifdef CONFIG_DMA_RESTRICTED_POOL
  921. struct page *swiotlb_alloc(struct device *dev, size_t size)
  922. {
  923. struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
  924. phys_addr_t tlb_addr;
  925. int index;
  926. if (!mem)
  927. return NULL;
  928. index = swiotlb_find_slots(dev, 0, size, 0);
  929. if (index == -1)
  930. return NULL;
  931. tlb_addr = slot_addr(mem->start, index);
  932. return pfn_to_page(PFN_DOWN(tlb_addr));
  933. }
  934. bool swiotlb_free(struct device *dev, struct page *page, size_t size)
  935. {
  936. phys_addr_t tlb_addr = page_to_phys(page);
  937. if (!is_swiotlb_buffer(dev, tlb_addr))
  938. return false;
  939. swiotlb_release_slots(dev, tlb_addr);
  940. return true;
  941. }
  942. static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
  943. struct device *dev)
  944. {
  945. struct io_tlb_mem *mem = rmem->priv;
  946. unsigned long nslabs = rmem->size >> IO_TLB_SHIFT;
  947. /* Set Per-device io tlb area to one */
  948. unsigned int nareas = 1;
  949. if (PageHighMem(pfn_to_page(PHYS_PFN(rmem->base)))) {
  950. dev_err(dev, "Restricted DMA pool must be accessible within the linear mapping.");
  951. return -EINVAL;
  952. }
  953. /*
  954. * Since multiple devices can share the same pool, the private data,
  955. * io_tlb_mem struct, will be initialized by the first device attached
  956. * to it.
  957. */
  958. if (!mem) {
  959. mem = kzalloc(sizeof(*mem), GFP_KERNEL);
  960. if (!mem)
  961. return -ENOMEM;
  962. mem->slots = kcalloc(nslabs, sizeof(*mem->slots), GFP_KERNEL);
  963. if (!mem->slots) {
  964. kfree(mem);
  965. return -ENOMEM;
  966. }
  967. mem->areas = kcalloc(nareas, sizeof(*mem->areas),
  968. GFP_KERNEL);
  969. if (!mem->areas) {
  970. kfree(mem->slots);
  971. kfree(mem);
  972. return -ENOMEM;
  973. }
  974. set_memory_decrypted((unsigned long)phys_to_virt(rmem->base),
  975. rmem->size >> PAGE_SHIFT);
  976. swiotlb_init_io_tlb_mem(mem, rmem->base, nslabs, SWIOTLB_FORCE,
  977. false, nareas);
  978. mem->for_alloc = true;
  979. rmem->priv = mem;
  980. swiotlb_create_debugfs_files(mem, rmem->name);
  981. }
  982. dev->dma_io_tlb_mem = mem;
  983. return 0;
  984. }
  985. static void rmem_swiotlb_device_release(struct reserved_mem *rmem,
  986. struct device *dev)
  987. {
  988. dev->dma_io_tlb_mem = &io_tlb_default_mem;
  989. }
  990. static const struct reserved_mem_ops rmem_swiotlb_ops = {
  991. .device_init = rmem_swiotlb_device_init,
  992. .device_release = rmem_swiotlb_device_release,
  993. };
  994. static int __init rmem_swiotlb_setup(struct reserved_mem *rmem)
  995. {
  996. unsigned long node = rmem->fdt_node;
  997. if (of_get_flat_dt_prop(node, "reusable", NULL) ||
  998. of_get_flat_dt_prop(node, "linux,cma-default", NULL) ||
  999. of_get_flat_dt_prop(node, "linux,dma-default", NULL) ||
  1000. of_get_flat_dt_prop(node, "no-map", NULL))
  1001. return -EINVAL;
  1002. rmem->ops = &rmem_swiotlb_ops;
  1003. pr_info("Reserved memory: created restricted DMA pool at %pa, size %ld MiB\n",
  1004. &rmem->base, (unsigned long)rmem->size / SZ_1M);
  1005. return 0;
  1006. }
  1007. RESERVEDMEM_OF_DECLARE(dma, "restricted-dma-pool", rmem_swiotlb_setup);
  1008. #endif /* CONFIG_DMA_RESTRICTED_POOL */