iova.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /*
  2. * Copyright © 2006-2009, Intel Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  15. * Place - Suite 330, Boston, MA 02111-1307 USA.
  16. *
  17. * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
  18. */
  19. #include <linux/iova.h>
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/smp.h>
  23. #include <linux/bitops.h>
  24. #include <linux/cpu.h>
  25. /* The anchor node sits above the top of the usable address space */
  26. #define IOVA_ANCHOR ~0UL
  27. static bool iova_rcache_insert(struct iova_domain *iovad,
  28. unsigned long pfn,
  29. unsigned long size);
  30. static unsigned long iova_rcache_get(struct iova_domain *iovad,
  31. unsigned long size,
  32. unsigned long limit_pfn);
  33. static void init_iova_rcaches(struct iova_domain *iovad);
  34. static void free_iova_rcaches(struct iova_domain *iovad);
  35. static void fq_destroy_all_entries(struct iova_domain *iovad);
  36. static void fq_flush_timeout(struct timer_list *t);
  37. void
  38. init_iova_domain(struct iova_domain *iovad, unsigned long granule,
  39. unsigned long start_pfn)
  40. {
  41. /*
  42. * IOVA granularity will normally be equal to the smallest
  43. * supported IOMMU page size; both *must* be capable of
  44. * representing individual CPU pages exactly.
  45. */
  46. BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule));
  47. spin_lock_init(&iovad->iova_rbtree_lock);
  48. iovad->rbroot = RB_ROOT;
  49. iovad->cached_node = &iovad->anchor.node;
  50. iovad->cached32_node = &iovad->anchor.node;
  51. iovad->granule = granule;
  52. iovad->start_pfn = start_pfn;
  53. iovad->dma_32bit_pfn = 1UL << (32 - iova_shift(iovad));
  54. iovad->max32_alloc_size = iovad->dma_32bit_pfn;
  55. iovad->flush_cb = NULL;
  56. iovad->fq = NULL;
  57. iovad->anchor.pfn_lo = iovad->anchor.pfn_hi = IOVA_ANCHOR;
  58. rb_link_node(&iovad->anchor.node, NULL, &iovad->rbroot.rb_node);
  59. rb_insert_color(&iovad->anchor.node, &iovad->rbroot);
  60. init_iova_rcaches(iovad);
  61. }
  62. EXPORT_SYMBOL_GPL(init_iova_domain);
  63. static void free_iova_flush_queue(struct iova_domain *iovad)
  64. {
  65. if (!iovad->fq)
  66. return;
  67. if (timer_pending(&iovad->fq_timer))
  68. del_timer(&iovad->fq_timer);
  69. fq_destroy_all_entries(iovad);
  70. free_percpu(iovad->fq);
  71. iovad->fq = NULL;
  72. iovad->flush_cb = NULL;
  73. iovad->entry_dtor = NULL;
  74. }
  75. int init_iova_flush_queue(struct iova_domain *iovad,
  76. iova_flush_cb flush_cb, iova_entry_dtor entry_dtor)
  77. {
  78. int cpu;
  79. atomic64_set(&iovad->fq_flush_start_cnt, 0);
  80. atomic64_set(&iovad->fq_flush_finish_cnt, 0);
  81. iovad->fq = alloc_percpu(struct iova_fq);
  82. if (!iovad->fq)
  83. return -ENOMEM;
  84. iovad->flush_cb = flush_cb;
  85. iovad->entry_dtor = entry_dtor;
  86. for_each_possible_cpu(cpu) {
  87. struct iova_fq *fq;
  88. fq = per_cpu_ptr(iovad->fq, cpu);
  89. fq->head = 0;
  90. fq->tail = 0;
  91. spin_lock_init(&fq->lock);
  92. }
  93. timer_setup(&iovad->fq_timer, fq_flush_timeout, 0);
  94. atomic_set(&iovad->fq_timer_on, 0);
  95. return 0;
  96. }
  97. EXPORT_SYMBOL_GPL(init_iova_flush_queue);
  98. static struct rb_node *
  99. __get_cached_rbnode(struct iova_domain *iovad, unsigned long limit_pfn)
  100. {
  101. if (limit_pfn <= iovad->dma_32bit_pfn)
  102. return iovad->cached32_node;
  103. return iovad->cached_node;
  104. }
  105. static void
  106. __cached_rbnode_insert_update(struct iova_domain *iovad, struct iova *new)
  107. {
  108. if (new->pfn_hi < iovad->dma_32bit_pfn)
  109. iovad->cached32_node = &new->node;
  110. else
  111. iovad->cached_node = &new->node;
  112. }
  113. static void
  114. __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
  115. {
  116. struct iova *cached_iova;
  117. cached_iova = rb_entry(iovad->cached32_node, struct iova, node);
  118. if (free->pfn_hi < iovad->dma_32bit_pfn &&
  119. free->pfn_lo >= cached_iova->pfn_lo) {
  120. iovad->cached32_node = rb_next(&free->node);
  121. iovad->max32_alloc_size = iovad->dma_32bit_pfn;
  122. }
  123. cached_iova = rb_entry(iovad->cached_node, struct iova, node);
  124. if (free->pfn_lo >= cached_iova->pfn_lo)
  125. iovad->cached_node = rb_next(&free->node);
  126. }
  127. /* Insert the iova into domain rbtree by holding writer lock */
  128. static void
  129. iova_insert_rbtree(struct rb_root *root, struct iova *iova,
  130. struct rb_node *start)
  131. {
  132. struct rb_node **new, *parent = NULL;
  133. new = (start) ? &start : &(root->rb_node);
  134. /* Figure out where to put new node */
  135. while (*new) {
  136. struct iova *this = rb_entry(*new, struct iova, node);
  137. parent = *new;
  138. if (iova->pfn_lo < this->pfn_lo)
  139. new = &((*new)->rb_left);
  140. else if (iova->pfn_lo > this->pfn_lo)
  141. new = &((*new)->rb_right);
  142. else {
  143. WARN_ON(1); /* this should not happen */
  144. return;
  145. }
  146. }
  147. /* Add new node and rebalance tree. */
  148. rb_link_node(&iova->node, parent, new);
  149. rb_insert_color(&iova->node, root);
  150. }
  151. static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
  152. unsigned long size, unsigned long limit_pfn,
  153. struct iova *new, bool size_aligned)
  154. {
  155. struct rb_node *curr, *prev;
  156. struct iova *curr_iova;
  157. unsigned long flags;
  158. unsigned long new_pfn;
  159. unsigned long align_mask = ~0UL;
  160. if (size_aligned)
  161. align_mask <<= fls_long(size - 1);
  162. /* Walk the tree backwards */
  163. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  164. if (limit_pfn <= iovad->dma_32bit_pfn &&
  165. size >= iovad->max32_alloc_size)
  166. goto iova32_full;
  167. curr = __get_cached_rbnode(iovad, limit_pfn);
  168. curr_iova = rb_entry(curr, struct iova, node);
  169. do {
  170. limit_pfn = min(limit_pfn, curr_iova->pfn_lo);
  171. new_pfn = (limit_pfn - size) & align_mask;
  172. prev = curr;
  173. curr = rb_prev(curr);
  174. curr_iova = rb_entry(curr, struct iova, node);
  175. } while (curr && new_pfn <= curr_iova->pfn_hi);
  176. if (limit_pfn < size || new_pfn < iovad->start_pfn)
  177. goto iova32_full;
  178. /* pfn_lo will point to size aligned address if size_aligned is set */
  179. new->pfn_lo = new_pfn;
  180. new->pfn_hi = new->pfn_lo + size - 1;
  181. /* If we have 'prev', it's a valid place to start the insertion. */
  182. iova_insert_rbtree(&iovad->rbroot, new, prev);
  183. __cached_rbnode_insert_update(iovad, new);
  184. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  185. return 0;
  186. iova32_full:
  187. iovad->max32_alloc_size = size;
  188. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  189. return -ENOMEM;
  190. }
  191. static struct kmem_cache *iova_cache;
  192. static unsigned int iova_cache_users;
  193. static DEFINE_MUTEX(iova_cache_mutex);
  194. struct iova *alloc_iova_mem(void)
  195. {
  196. return kmem_cache_alloc(iova_cache, GFP_ATOMIC);
  197. }
  198. EXPORT_SYMBOL(alloc_iova_mem);
  199. void free_iova_mem(struct iova *iova)
  200. {
  201. if (iova->pfn_lo != IOVA_ANCHOR)
  202. kmem_cache_free(iova_cache, iova);
  203. }
  204. EXPORT_SYMBOL(free_iova_mem);
  205. int iova_cache_get(void)
  206. {
  207. mutex_lock(&iova_cache_mutex);
  208. if (!iova_cache_users) {
  209. iova_cache = kmem_cache_create(
  210. "iommu_iova", sizeof(struct iova), 0,
  211. SLAB_HWCACHE_ALIGN, NULL);
  212. if (!iova_cache) {
  213. mutex_unlock(&iova_cache_mutex);
  214. printk(KERN_ERR "Couldn't create iova cache\n");
  215. return -ENOMEM;
  216. }
  217. }
  218. iova_cache_users++;
  219. mutex_unlock(&iova_cache_mutex);
  220. return 0;
  221. }
  222. EXPORT_SYMBOL_GPL(iova_cache_get);
  223. void iova_cache_put(void)
  224. {
  225. mutex_lock(&iova_cache_mutex);
  226. if (WARN_ON(!iova_cache_users)) {
  227. mutex_unlock(&iova_cache_mutex);
  228. return;
  229. }
  230. iova_cache_users--;
  231. if (!iova_cache_users)
  232. kmem_cache_destroy(iova_cache);
  233. mutex_unlock(&iova_cache_mutex);
  234. }
  235. EXPORT_SYMBOL_GPL(iova_cache_put);
  236. /**
  237. * alloc_iova - allocates an iova
  238. * @iovad: - iova domain in question
  239. * @size: - size of page frames to allocate
  240. * @limit_pfn: - max limit address
  241. * @size_aligned: - set if size_aligned address range is required
  242. * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
  243. * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
  244. * flag is set then the allocated address iova->pfn_lo will be naturally
  245. * aligned on roundup_power_of_two(size).
  246. */
  247. struct iova *
  248. alloc_iova(struct iova_domain *iovad, unsigned long size,
  249. unsigned long limit_pfn,
  250. bool size_aligned)
  251. {
  252. struct iova *new_iova;
  253. int ret;
  254. new_iova = alloc_iova_mem();
  255. if (!new_iova)
  256. return NULL;
  257. ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn + 1,
  258. new_iova, size_aligned);
  259. if (ret) {
  260. free_iova_mem(new_iova);
  261. return NULL;
  262. }
  263. return new_iova;
  264. }
  265. EXPORT_SYMBOL_GPL(alloc_iova);
  266. static struct iova *
  267. private_find_iova(struct iova_domain *iovad, unsigned long pfn)
  268. {
  269. struct rb_node *node = iovad->rbroot.rb_node;
  270. assert_spin_locked(&iovad->iova_rbtree_lock);
  271. while (node) {
  272. struct iova *iova = rb_entry(node, struct iova, node);
  273. if (pfn < iova->pfn_lo)
  274. node = node->rb_left;
  275. else if (pfn > iova->pfn_hi)
  276. node = node->rb_right;
  277. else
  278. return iova; /* pfn falls within iova's range */
  279. }
  280. return NULL;
  281. }
  282. static void private_free_iova(struct iova_domain *iovad, struct iova *iova)
  283. {
  284. assert_spin_locked(&iovad->iova_rbtree_lock);
  285. __cached_rbnode_delete_update(iovad, iova);
  286. rb_erase(&iova->node, &iovad->rbroot);
  287. free_iova_mem(iova);
  288. }
  289. /**
  290. * find_iova - finds an iova for a given pfn
  291. * @iovad: - iova domain in question.
  292. * @pfn: - page frame number
  293. * This function finds and returns an iova belonging to the
  294. * given doamin which matches the given pfn.
  295. */
  296. struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
  297. {
  298. unsigned long flags;
  299. struct iova *iova;
  300. /* Take the lock so that no other thread is manipulating the rbtree */
  301. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  302. iova = private_find_iova(iovad, pfn);
  303. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  304. return iova;
  305. }
  306. EXPORT_SYMBOL_GPL(find_iova);
  307. /**
  308. * __free_iova - frees the given iova
  309. * @iovad: iova domain in question.
  310. * @iova: iova in question.
  311. * Frees the given iova belonging to the giving domain
  312. */
  313. void
  314. __free_iova(struct iova_domain *iovad, struct iova *iova)
  315. {
  316. unsigned long flags;
  317. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  318. private_free_iova(iovad, iova);
  319. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  320. }
  321. EXPORT_SYMBOL_GPL(__free_iova);
  322. /**
  323. * free_iova - finds and frees the iova for a given pfn
  324. * @iovad: - iova domain in question.
  325. * @pfn: - pfn that is allocated previously
  326. * This functions finds an iova for a given pfn and then
  327. * frees the iova from that domain.
  328. */
  329. void
  330. free_iova(struct iova_domain *iovad, unsigned long pfn)
  331. {
  332. struct iova *iova = find_iova(iovad, pfn);
  333. if (iova)
  334. __free_iova(iovad, iova);
  335. }
  336. EXPORT_SYMBOL_GPL(free_iova);
  337. /**
  338. * alloc_iova_fast - allocates an iova from rcache
  339. * @iovad: - iova domain in question
  340. * @size: - size of page frames to allocate
  341. * @limit_pfn: - max limit address
  342. * @flush_rcache: - set to flush rcache on regular allocation failure
  343. * This function tries to satisfy an iova allocation from the rcache,
  344. * and falls back to regular allocation on failure. If regular allocation
  345. * fails too and the flush_rcache flag is set then the rcache will be flushed.
  346. */
  347. unsigned long
  348. alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
  349. unsigned long limit_pfn, bool flush_rcache)
  350. {
  351. unsigned long iova_pfn;
  352. struct iova *new_iova;
  353. iova_pfn = iova_rcache_get(iovad, size, limit_pfn + 1);
  354. if (iova_pfn)
  355. return iova_pfn;
  356. retry:
  357. new_iova = alloc_iova(iovad, size, limit_pfn, true);
  358. if (!new_iova) {
  359. unsigned int cpu;
  360. if (!flush_rcache)
  361. return 0;
  362. /* Try replenishing IOVAs by flushing rcache. */
  363. flush_rcache = false;
  364. for_each_online_cpu(cpu)
  365. free_cpu_cached_iovas(cpu, iovad);
  366. goto retry;
  367. }
  368. return new_iova->pfn_lo;
  369. }
  370. EXPORT_SYMBOL_GPL(alloc_iova_fast);
  371. /**
  372. * free_iova_fast - free iova pfn range into rcache
  373. * @iovad: - iova domain in question.
  374. * @pfn: - pfn that is allocated previously
  375. * @size: - # of pages in range
  376. * This functions frees an iova range by trying to put it into the rcache,
  377. * falling back to regular iova deallocation via free_iova() if this fails.
  378. */
  379. void
  380. free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
  381. {
  382. if (iova_rcache_insert(iovad, pfn, size))
  383. return;
  384. free_iova(iovad, pfn);
  385. }
  386. EXPORT_SYMBOL_GPL(free_iova_fast);
  387. #define fq_ring_for_each(i, fq) \
  388. for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE)
  389. static inline bool fq_full(struct iova_fq *fq)
  390. {
  391. assert_spin_locked(&fq->lock);
  392. return (((fq->tail + 1) % IOVA_FQ_SIZE) == fq->head);
  393. }
  394. static inline unsigned fq_ring_add(struct iova_fq *fq)
  395. {
  396. unsigned idx = fq->tail;
  397. assert_spin_locked(&fq->lock);
  398. fq->tail = (idx + 1) % IOVA_FQ_SIZE;
  399. return idx;
  400. }
  401. static void fq_ring_free(struct iova_domain *iovad, struct iova_fq *fq)
  402. {
  403. u64 counter = atomic64_read(&iovad->fq_flush_finish_cnt);
  404. unsigned idx;
  405. assert_spin_locked(&fq->lock);
  406. fq_ring_for_each(idx, fq) {
  407. if (fq->entries[idx].counter >= counter)
  408. break;
  409. if (iovad->entry_dtor)
  410. iovad->entry_dtor(fq->entries[idx].data);
  411. free_iova_fast(iovad,
  412. fq->entries[idx].iova_pfn,
  413. fq->entries[idx].pages);
  414. fq->head = (fq->head + 1) % IOVA_FQ_SIZE;
  415. }
  416. }
  417. static void iova_domain_flush(struct iova_domain *iovad)
  418. {
  419. atomic64_inc(&iovad->fq_flush_start_cnt);
  420. iovad->flush_cb(iovad);
  421. atomic64_inc(&iovad->fq_flush_finish_cnt);
  422. }
  423. static void fq_destroy_all_entries(struct iova_domain *iovad)
  424. {
  425. int cpu;
  426. /*
  427. * This code runs when the iova_domain is being detroyed, so don't
  428. * bother to free iovas, just call the entry_dtor on all remaining
  429. * entries.
  430. */
  431. if (!iovad->entry_dtor)
  432. return;
  433. for_each_possible_cpu(cpu) {
  434. struct iova_fq *fq = per_cpu_ptr(iovad->fq, cpu);
  435. int idx;
  436. fq_ring_for_each(idx, fq)
  437. iovad->entry_dtor(fq->entries[idx].data);
  438. }
  439. }
  440. static void fq_flush_timeout(struct timer_list *t)
  441. {
  442. struct iova_domain *iovad = from_timer(iovad, t, fq_timer);
  443. int cpu;
  444. atomic_set(&iovad->fq_timer_on, 0);
  445. iova_domain_flush(iovad);
  446. for_each_possible_cpu(cpu) {
  447. unsigned long flags;
  448. struct iova_fq *fq;
  449. fq = per_cpu_ptr(iovad->fq, cpu);
  450. spin_lock_irqsave(&fq->lock, flags);
  451. fq_ring_free(iovad, fq);
  452. spin_unlock_irqrestore(&fq->lock, flags);
  453. }
  454. }
  455. void queue_iova(struct iova_domain *iovad,
  456. unsigned long pfn, unsigned long pages,
  457. unsigned long data)
  458. {
  459. struct iova_fq *fq = raw_cpu_ptr(iovad->fq);
  460. unsigned long flags;
  461. unsigned idx;
  462. spin_lock_irqsave(&fq->lock, flags);
  463. /*
  464. * First remove all entries from the flush queue that have already been
  465. * flushed out on another CPU. This makes the fq_full() check below less
  466. * likely to be true.
  467. */
  468. fq_ring_free(iovad, fq);
  469. if (fq_full(fq)) {
  470. iova_domain_flush(iovad);
  471. fq_ring_free(iovad, fq);
  472. }
  473. idx = fq_ring_add(fq);
  474. fq->entries[idx].iova_pfn = pfn;
  475. fq->entries[idx].pages = pages;
  476. fq->entries[idx].data = data;
  477. fq->entries[idx].counter = atomic64_read(&iovad->fq_flush_start_cnt);
  478. spin_unlock_irqrestore(&fq->lock, flags);
  479. if (atomic_cmpxchg(&iovad->fq_timer_on, 0, 1) == 0)
  480. mod_timer(&iovad->fq_timer,
  481. jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT));
  482. }
  483. EXPORT_SYMBOL_GPL(queue_iova);
  484. /**
  485. * put_iova_domain - destroys the iova doamin
  486. * @iovad: - iova domain in question.
  487. * All the iova's in that domain are destroyed.
  488. */
  489. void put_iova_domain(struct iova_domain *iovad)
  490. {
  491. struct iova *iova, *tmp;
  492. free_iova_flush_queue(iovad);
  493. free_iova_rcaches(iovad);
  494. rbtree_postorder_for_each_entry_safe(iova, tmp, &iovad->rbroot, node)
  495. free_iova_mem(iova);
  496. }
  497. EXPORT_SYMBOL_GPL(put_iova_domain);
  498. static int
  499. __is_range_overlap(struct rb_node *node,
  500. unsigned long pfn_lo, unsigned long pfn_hi)
  501. {
  502. struct iova *iova = rb_entry(node, struct iova, node);
  503. if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
  504. return 1;
  505. return 0;
  506. }
  507. static inline struct iova *
  508. alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
  509. {
  510. struct iova *iova;
  511. iova = alloc_iova_mem();
  512. if (iova) {
  513. iova->pfn_lo = pfn_lo;
  514. iova->pfn_hi = pfn_hi;
  515. }
  516. return iova;
  517. }
  518. static struct iova *
  519. __insert_new_range(struct iova_domain *iovad,
  520. unsigned long pfn_lo, unsigned long pfn_hi)
  521. {
  522. struct iova *iova;
  523. iova = alloc_and_init_iova(pfn_lo, pfn_hi);
  524. if (iova)
  525. iova_insert_rbtree(&iovad->rbroot, iova, NULL);
  526. return iova;
  527. }
  528. static void
  529. __adjust_overlap_range(struct iova *iova,
  530. unsigned long *pfn_lo, unsigned long *pfn_hi)
  531. {
  532. if (*pfn_lo < iova->pfn_lo)
  533. iova->pfn_lo = *pfn_lo;
  534. if (*pfn_hi > iova->pfn_hi)
  535. *pfn_lo = iova->pfn_hi + 1;
  536. }
  537. /**
  538. * reserve_iova - reserves an iova in the given range
  539. * @iovad: - iova domain pointer
  540. * @pfn_lo: - lower page frame address
  541. * @pfn_hi:- higher pfn adderss
  542. * This function allocates reserves the address range from pfn_lo to pfn_hi so
  543. * that this address is not dished out as part of alloc_iova.
  544. */
  545. struct iova *
  546. reserve_iova(struct iova_domain *iovad,
  547. unsigned long pfn_lo, unsigned long pfn_hi)
  548. {
  549. struct rb_node *node;
  550. unsigned long flags;
  551. struct iova *iova;
  552. unsigned int overlap = 0;
  553. /* Don't allow nonsensical pfns */
  554. if (WARN_ON((pfn_hi | pfn_lo) > (ULLONG_MAX >> iova_shift(iovad))))
  555. return NULL;
  556. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  557. for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
  558. if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
  559. iova = rb_entry(node, struct iova, node);
  560. __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
  561. if ((pfn_lo >= iova->pfn_lo) &&
  562. (pfn_hi <= iova->pfn_hi))
  563. goto finish;
  564. overlap = 1;
  565. } else if (overlap)
  566. break;
  567. }
  568. /* We are here either because this is the first reserver node
  569. * or need to insert remaining non overlap addr range
  570. */
  571. iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
  572. finish:
  573. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  574. return iova;
  575. }
  576. EXPORT_SYMBOL_GPL(reserve_iova);
  577. /**
  578. * copy_reserved_iova - copies the reserved between domains
  579. * @from: - source doamin from where to copy
  580. * @to: - destination domin where to copy
  581. * This function copies reserved iova's from one doamin to
  582. * other.
  583. */
  584. void
  585. copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
  586. {
  587. unsigned long flags;
  588. struct rb_node *node;
  589. spin_lock_irqsave(&from->iova_rbtree_lock, flags);
  590. for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
  591. struct iova *iova = rb_entry(node, struct iova, node);
  592. struct iova *new_iova;
  593. if (iova->pfn_lo == IOVA_ANCHOR)
  594. continue;
  595. new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
  596. if (!new_iova)
  597. printk(KERN_ERR "Reserve iova range %lx@%lx failed\n",
  598. iova->pfn_lo, iova->pfn_lo);
  599. }
  600. spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
  601. }
  602. EXPORT_SYMBOL_GPL(copy_reserved_iova);
  603. struct iova *
  604. split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
  605. unsigned long pfn_lo, unsigned long pfn_hi)
  606. {
  607. unsigned long flags;
  608. struct iova *prev = NULL, *next = NULL;
  609. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  610. if (iova->pfn_lo < pfn_lo) {
  611. prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1);
  612. if (prev == NULL)
  613. goto error;
  614. }
  615. if (iova->pfn_hi > pfn_hi) {
  616. next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi);
  617. if (next == NULL)
  618. goto error;
  619. }
  620. __cached_rbnode_delete_update(iovad, iova);
  621. rb_erase(&iova->node, &iovad->rbroot);
  622. if (prev) {
  623. iova_insert_rbtree(&iovad->rbroot, prev, NULL);
  624. iova->pfn_lo = pfn_lo;
  625. }
  626. if (next) {
  627. iova_insert_rbtree(&iovad->rbroot, next, NULL);
  628. iova->pfn_hi = pfn_hi;
  629. }
  630. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  631. return iova;
  632. error:
  633. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  634. if (prev)
  635. free_iova_mem(prev);
  636. return NULL;
  637. }
  638. /*
  639. * Magazine caches for IOVA ranges. For an introduction to magazines,
  640. * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
  641. * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
  642. * For simplicity, we use a static magazine size and don't implement the
  643. * dynamic size tuning described in the paper.
  644. */
  645. #define IOVA_MAG_SIZE 128
  646. struct iova_magazine {
  647. unsigned long size;
  648. unsigned long pfns[IOVA_MAG_SIZE];
  649. };
  650. struct iova_cpu_rcache {
  651. spinlock_t lock;
  652. struct iova_magazine *loaded;
  653. struct iova_magazine *prev;
  654. };
  655. static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
  656. {
  657. return kzalloc(sizeof(struct iova_magazine), flags);
  658. }
  659. static void iova_magazine_free(struct iova_magazine *mag)
  660. {
  661. kfree(mag);
  662. }
  663. static void
  664. iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
  665. {
  666. unsigned long flags;
  667. int i;
  668. if (!mag)
  669. return;
  670. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  671. for (i = 0 ; i < mag->size; ++i) {
  672. struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
  673. BUG_ON(!iova);
  674. private_free_iova(iovad, iova);
  675. }
  676. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  677. mag->size = 0;
  678. }
  679. static bool iova_magazine_full(struct iova_magazine *mag)
  680. {
  681. return (mag && mag->size == IOVA_MAG_SIZE);
  682. }
  683. static bool iova_magazine_empty(struct iova_magazine *mag)
  684. {
  685. return (!mag || mag->size == 0);
  686. }
  687. static unsigned long iova_magazine_pop(struct iova_magazine *mag,
  688. unsigned long limit_pfn)
  689. {
  690. int i;
  691. unsigned long pfn;
  692. BUG_ON(iova_magazine_empty(mag));
  693. /* Only fall back to the rbtree if we have no suitable pfns at all */
  694. for (i = mag->size - 1; mag->pfns[i] > limit_pfn; i--)
  695. if (i == 0)
  696. return 0;
  697. /* Swap it to pop it */
  698. pfn = mag->pfns[i];
  699. mag->pfns[i] = mag->pfns[--mag->size];
  700. return pfn;
  701. }
  702. static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
  703. {
  704. BUG_ON(iova_magazine_full(mag));
  705. mag->pfns[mag->size++] = pfn;
  706. }
  707. static void init_iova_rcaches(struct iova_domain *iovad)
  708. {
  709. struct iova_cpu_rcache *cpu_rcache;
  710. struct iova_rcache *rcache;
  711. unsigned int cpu;
  712. int i;
  713. for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
  714. rcache = &iovad->rcaches[i];
  715. spin_lock_init(&rcache->lock);
  716. rcache->depot_size = 0;
  717. rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());
  718. if (WARN_ON(!rcache->cpu_rcaches))
  719. continue;
  720. for_each_possible_cpu(cpu) {
  721. cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
  722. spin_lock_init(&cpu_rcache->lock);
  723. cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
  724. cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
  725. }
  726. }
  727. }
  728. /*
  729. * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
  730. * return true on success. Can fail if rcache is full and we can't free
  731. * space, and free_iova() (our only caller) will then return the IOVA
  732. * range to the rbtree instead.
  733. */
  734. static bool __iova_rcache_insert(struct iova_domain *iovad,
  735. struct iova_rcache *rcache,
  736. unsigned long iova_pfn)
  737. {
  738. struct iova_magazine *mag_to_free = NULL;
  739. struct iova_cpu_rcache *cpu_rcache;
  740. bool can_insert = false;
  741. unsigned long flags;
  742. cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
  743. spin_lock_irqsave(&cpu_rcache->lock, flags);
  744. if (!iova_magazine_full(cpu_rcache->loaded)) {
  745. can_insert = true;
  746. } else if (!iova_magazine_full(cpu_rcache->prev)) {
  747. swap(cpu_rcache->prev, cpu_rcache->loaded);
  748. can_insert = true;
  749. } else {
  750. struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
  751. if (new_mag) {
  752. spin_lock(&rcache->lock);
  753. if (rcache->depot_size < MAX_GLOBAL_MAGS) {
  754. rcache->depot[rcache->depot_size++] =
  755. cpu_rcache->loaded;
  756. } else {
  757. mag_to_free = cpu_rcache->loaded;
  758. }
  759. spin_unlock(&rcache->lock);
  760. cpu_rcache->loaded = new_mag;
  761. can_insert = true;
  762. }
  763. }
  764. if (can_insert)
  765. iova_magazine_push(cpu_rcache->loaded, iova_pfn);
  766. spin_unlock_irqrestore(&cpu_rcache->lock, flags);
  767. if (mag_to_free) {
  768. iova_magazine_free_pfns(mag_to_free, iovad);
  769. iova_magazine_free(mag_to_free);
  770. }
  771. return can_insert;
  772. }
  773. static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
  774. unsigned long size)
  775. {
  776. unsigned int log_size = order_base_2(size);
  777. if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
  778. return false;
  779. return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
  780. }
  781. /*
  782. * Caller wants to allocate a new IOVA range from 'rcache'. If we can
  783. * satisfy the request, return a matching non-NULL range and remove
  784. * it from the 'rcache'.
  785. */
  786. static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
  787. unsigned long limit_pfn)
  788. {
  789. struct iova_cpu_rcache *cpu_rcache;
  790. unsigned long iova_pfn = 0;
  791. bool has_pfn = false;
  792. unsigned long flags;
  793. cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
  794. spin_lock_irqsave(&cpu_rcache->lock, flags);
  795. if (!iova_magazine_empty(cpu_rcache->loaded)) {
  796. has_pfn = true;
  797. } else if (!iova_magazine_empty(cpu_rcache->prev)) {
  798. swap(cpu_rcache->prev, cpu_rcache->loaded);
  799. has_pfn = true;
  800. } else {
  801. spin_lock(&rcache->lock);
  802. if (rcache->depot_size > 0) {
  803. iova_magazine_free(cpu_rcache->loaded);
  804. cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
  805. has_pfn = true;
  806. }
  807. spin_unlock(&rcache->lock);
  808. }
  809. if (has_pfn)
  810. iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
  811. spin_unlock_irqrestore(&cpu_rcache->lock, flags);
  812. return iova_pfn;
  813. }
  814. /*
  815. * Try to satisfy IOVA allocation range from rcache. Fail if requested
  816. * size is too big or the DMA limit we are given isn't satisfied by the
  817. * top element in the magazine.
  818. */
  819. static unsigned long iova_rcache_get(struct iova_domain *iovad,
  820. unsigned long size,
  821. unsigned long limit_pfn)
  822. {
  823. unsigned int log_size = order_base_2(size);
  824. if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
  825. return 0;
  826. return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn - size);
  827. }
  828. /*
  829. * free rcache data structures.
  830. */
  831. static void free_iova_rcaches(struct iova_domain *iovad)
  832. {
  833. struct iova_rcache *rcache;
  834. struct iova_cpu_rcache *cpu_rcache;
  835. unsigned int cpu;
  836. int i, j;
  837. for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
  838. rcache = &iovad->rcaches[i];
  839. for_each_possible_cpu(cpu) {
  840. cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
  841. iova_magazine_free(cpu_rcache->loaded);
  842. iova_magazine_free(cpu_rcache->prev);
  843. }
  844. free_percpu(rcache->cpu_rcaches);
  845. for (j = 0; j < rcache->depot_size; ++j)
  846. iova_magazine_free(rcache->depot[j]);
  847. }
  848. }
  849. /*
  850. * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
  851. */
  852. void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
  853. {
  854. struct iova_cpu_rcache *cpu_rcache;
  855. struct iova_rcache *rcache;
  856. unsigned long flags;
  857. int i;
  858. for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
  859. rcache = &iovad->rcaches[i];
  860. cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
  861. spin_lock_irqsave(&cpu_rcache->lock, flags);
  862. iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
  863. iova_magazine_free_pfns(cpu_rcache->prev, iovad);
  864. spin_unlock_irqrestore(&cpu_rcache->lock, flags);
  865. }
  866. }
  867. MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
  868. MODULE_LICENSE("GPL");