iova.c 27 KB

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