iova.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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->flush_cb = NULL;
  55. iovad->fq = NULL;
  56. iovad->anchor.pfn_lo = iovad->anchor.pfn_hi = IOVA_ANCHOR;
  57. rb_link_node(&iovad->anchor.node, NULL, &iovad->rbroot.rb_node);
  58. rb_insert_color(&iovad->anchor.node, &iovad->rbroot);
  59. init_iova_rcaches(iovad);
  60. }
  61. EXPORT_SYMBOL_GPL(init_iova_domain);
  62. bool has_iova_flush_queue(struct iova_domain *iovad)
  63. {
  64. return !!iovad->fq;
  65. }
  66. static void free_iova_flush_queue(struct iova_domain *iovad)
  67. {
  68. if (!has_iova_flush_queue(iovad))
  69. return;
  70. if (timer_pending(&iovad->fq_timer))
  71. del_timer(&iovad->fq_timer);
  72. fq_destroy_all_entries(iovad);
  73. free_percpu(iovad->fq);
  74. iovad->fq = NULL;
  75. iovad->flush_cb = NULL;
  76. iovad->entry_dtor = NULL;
  77. }
  78. int init_iova_flush_queue(struct iova_domain *iovad,
  79. iova_flush_cb flush_cb, iova_entry_dtor entry_dtor)
  80. {
  81. struct iova_fq __percpu *queue;
  82. int cpu;
  83. atomic64_set(&iovad->fq_flush_start_cnt, 0);
  84. atomic64_set(&iovad->fq_flush_finish_cnt, 0);
  85. queue = alloc_percpu(struct iova_fq);
  86. if (!queue)
  87. return -ENOMEM;
  88. iovad->flush_cb = flush_cb;
  89. iovad->entry_dtor = entry_dtor;
  90. for_each_possible_cpu(cpu) {
  91. struct iova_fq *fq;
  92. fq = per_cpu_ptr(queue, cpu);
  93. fq->head = 0;
  94. fq->tail = 0;
  95. spin_lock_init(&fq->lock);
  96. }
  97. smp_wmb();
  98. iovad->fq = queue;
  99. timer_setup(&iovad->fq_timer, fq_flush_timeout, 0);
  100. atomic_set(&iovad->fq_timer_on, 0);
  101. return 0;
  102. }
  103. EXPORT_SYMBOL_GPL(init_iova_flush_queue);
  104. static struct rb_node *
  105. __get_cached_rbnode(struct iova_domain *iovad, unsigned long limit_pfn)
  106. {
  107. if (limit_pfn <= iovad->dma_32bit_pfn)
  108. return iovad->cached32_node;
  109. return iovad->cached_node;
  110. }
  111. static void
  112. __cached_rbnode_insert_update(struct iova_domain *iovad, struct iova *new)
  113. {
  114. if (new->pfn_hi < iovad->dma_32bit_pfn)
  115. iovad->cached32_node = &new->node;
  116. else
  117. iovad->cached_node = &new->node;
  118. }
  119. static void
  120. __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
  121. {
  122. struct iova *cached_iova;
  123. cached_iova = rb_entry(iovad->cached32_node, struct iova, node);
  124. if (free == cached_iova ||
  125. (free->pfn_hi < iovad->dma_32bit_pfn &&
  126. free->pfn_lo >= cached_iova->pfn_lo))
  127. iovad->cached32_node = rb_next(&free->node);
  128. cached_iova = rb_entry(iovad->cached_node, struct iova, node);
  129. if (free->pfn_lo >= cached_iova->pfn_lo)
  130. iovad->cached_node = rb_next(&free->node);
  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. static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
  157. unsigned long size, unsigned long limit_pfn,
  158. struct iova *new, bool size_aligned)
  159. {
  160. struct rb_node *curr, *prev;
  161. struct iova *curr_iova;
  162. unsigned long flags;
  163. unsigned long new_pfn;
  164. unsigned long align_mask = ~0UL;
  165. if (size_aligned)
  166. align_mask <<= fls_long(size - 1);
  167. /* Walk the tree backwards */
  168. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  169. curr = __get_cached_rbnode(iovad, limit_pfn);
  170. curr_iova = rb_entry(curr, struct iova, node);
  171. do {
  172. limit_pfn = min(limit_pfn, curr_iova->pfn_lo);
  173. new_pfn = (limit_pfn - size) & align_mask;
  174. prev = curr;
  175. curr = rb_prev(curr);
  176. curr_iova = rb_entry(curr, struct iova, node);
  177. } while (curr && new_pfn <= curr_iova->pfn_hi);
  178. if (limit_pfn < size || new_pfn < iovad->start_pfn) {
  179. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  180. return -ENOMEM;
  181. }
  182. /* pfn_lo will point to size aligned address if size_aligned is set */
  183. new->pfn_lo = new_pfn;
  184. new->pfn_hi = new->pfn_lo + size - 1;
  185. /* If we have 'prev', it's a valid place to start the insertion. */
  186. iova_insert_rbtree(&iovad->rbroot, new, prev);
  187. __cached_rbnode_insert_update(iovad, new);
  188. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  189. return 0;
  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_zalloc(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. /* Avoid false sharing as much as possible. */
  480. if (!atomic_read(&iovad->fq_timer_on) &&
  481. !atomic_cmpxchg(&iovad->fq_timer_on, 0, 1))
  482. mod_timer(&iovad->fq_timer,
  483. jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT));
  484. }
  485. EXPORT_SYMBOL_GPL(queue_iova);
  486. /**
  487. * put_iova_domain - destroys the iova doamin
  488. * @iovad: - iova domain in question.
  489. * All the iova's in that domain are destroyed.
  490. */
  491. void put_iova_domain(struct iova_domain *iovad)
  492. {
  493. struct iova *iova, *tmp;
  494. free_iova_flush_queue(iovad);
  495. free_iova_rcaches(iovad);
  496. rbtree_postorder_for_each_entry_safe(iova, tmp, &iovad->rbroot, node)
  497. free_iova_mem(iova);
  498. }
  499. EXPORT_SYMBOL_GPL(put_iova_domain);
  500. static int
  501. __is_range_overlap(struct rb_node *node,
  502. unsigned long pfn_lo, unsigned long pfn_hi)
  503. {
  504. struct iova *iova = rb_entry(node, struct iova, node);
  505. if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
  506. return 1;
  507. return 0;
  508. }
  509. static inline struct iova *
  510. alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
  511. {
  512. struct iova *iova;
  513. iova = alloc_iova_mem();
  514. if (iova) {
  515. iova->pfn_lo = pfn_lo;
  516. iova->pfn_hi = pfn_hi;
  517. }
  518. return iova;
  519. }
  520. static struct iova *
  521. __insert_new_range(struct iova_domain *iovad,
  522. unsigned long pfn_lo, unsigned long pfn_hi)
  523. {
  524. struct iova *iova;
  525. iova = alloc_and_init_iova(pfn_lo, pfn_hi);
  526. if (iova)
  527. iova_insert_rbtree(&iovad->rbroot, iova, NULL);
  528. return iova;
  529. }
  530. static void
  531. __adjust_overlap_range(struct iova *iova,
  532. unsigned long *pfn_lo, unsigned long *pfn_hi)
  533. {
  534. if (*pfn_lo < iova->pfn_lo)
  535. iova->pfn_lo = *pfn_lo;
  536. if (*pfn_hi > iova->pfn_hi)
  537. *pfn_lo = iova->pfn_hi + 1;
  538. }
  539. /**
  540. * reserve_iova - reserves an iova in the given range
  541. * @iovad: - iova domain pointer
  542. * @pfn_lo: - lower page frame address
  543. * @pfn_hi:- higher pfn adderss
  544. * This function allocates reserves the address range from pfn_lo to pfn_hi so
  545. * that this address is not dished out as part of alloc_iova.
  546. */
  547. struct iova *
  548. reserve_iova(struct iova_domain *iovad,
  549. unsigned long pfn_lo, unsigned long pfn_hi)
  550. {
  551. struct rb_node *node;
  552. unsigned long flags;
  553. struct iova *iova;
  554. unsigned int overlap = 0;
  555. /* Don't allow nonsensical pfns */
  556. if (WARN_ON((pfn_hi | pfn_lo) > (ULLONG_MAX >> iova_shift(iovad))))
  557. return NULL;
  558. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  559. for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
  560. if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
  561. iova = rb_entry(node, struct iova, node);
  562. __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
  563. if ((pfn_lo >= iova->pfn_lo) &&
  564. (pfn_hi <= iova->pfn_hi))
  565. goto finish;
  566. overlap = 1;
  567. } else if (overlap)
  568. break;
  569. }
  570. /* We are here either because this is the first reserver node
  571. * or need to insert remaining non overlap addr range
  572. */
  573. iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
  574. finish:
  575. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  576. return iova;
  577. }
  578. EXPORT_SYMBOL_GPL(reserve_iova);
  579. /**
  580. * copy_reserved_iova - copies the reserved between domains
  581. * @from: - source doamin from where to copy
  582. * @to: - destination domin where to copy
  583. * This function copies reserved iova's from one doamin to
  584. * other.
  585. */
  586. void
  587. copy_reserved_iova(struct iova_domain *from, struct iova_domain *to)
  588. {
  589. unsigned long flags;
  590. struct rb_node *node;
  591. spin_lock_irqsave(&from->iova_rbtree_lock, flags);
  592. for (node = rb_first(&from->rbroot); node; node = rb_next(node)) {
  593. struct iova *iova = rb_entry(node, struct iova, node);
  594. struct iova *new_iova;
  595. if (iova->pfn_lo == IOVA_ANCHOR)
  596. continue;
  597. new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi);
  598. if (!new_iova)
  599. printk(KERN_ERR "Reserve iova range %lx@%lx failed\n",
  600. iova->pfn_lo, iova->pfn_lo);
  601. }
  602. spin_unlock_irqrestore(&from->iova_rbtree_lock, flags);
  603. }
  604. EXPORT_SYMBOL_GPL(copy_reserved_iova);
  605. struct iova *
  606. split_and_remove_iova(struct iova_domain *iovad, struct iova *iova,
  607. unsigned long pfn_lo, unsigned long pfn_hi)
  608. {
  609. unsigned long flags;
  610. struct iova *prev = NULL, *next = NULL;
  611. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  612. if (iova->pfn_lo < pfn_lo) {
  613. prev = alloc_and_init_iova(iova->pfn_lo, pfn_lo - 1);
  614. if (prev == NULL)
  615. goto error;
  616. }
  617. if (iova->pfn_hi > pfn_hi) {
  618. next = alloc_and_init_iova(pfn_hi + 1, iova->pfn_hi);
  619. if (next == NULL)
  620. goto error;
  621. }
  622. __cached_rbnode_delete_update(iovad, iova);
  623. rb_erase(&iova->node, &iovad->rbroot);
  624. if (prev) {
  625. iova_insert_rbtree(&iovad->rbroot, prev, NULL);
  626. iova->pfn_lo = pfn_lo;
  627. }
  628. if (next) {
  629. iova_insert_rbtree(&iovad->rbroot, next, NULL);
  630. iova->pfn_hi = pfn_hi;
  631. }
  632. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  633. return iova;
  634. error:
  635. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  636. if (prev)
  637. free_iova_mem(prev);
  638. return NULL;
  639. }
  640. /*
  641. * Magazine caches for IOVA ranges. For an introduction to magazines,
  642. * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
  643. * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
  644. * For simplicity, we use a static magazine size and don't implement the
  645. * dynamic size tuning described in the paper.
  646. */
  647. #define IOVA_MAG_SIZE 128
  648. struct iova_magazine {
  649. unsigned long size;
  650. unsigned long pfns[IOVA_MAG_SIZE];
  651. };
  652. struct iova_cpu_rcache {
  653. spinlock_t lock;
  654. struct iova_magazine *loaded;
  655. struct iova_magazine *prev;
  656. };
  657. static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
  658. {
  659. return kzalloc(sizeof(struct iova_magazine), flags);
  660. }
  661. static void iova_magazine_free(struct iova_magazine *mag)
  662. {
  663. kfree(mag);
  664. }
  665. static void
  666. iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
  667. {
  668. unsigned long flags;
  669. int i;
  670. if (!mag)
  671. return;
  672. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  673. for (i = 0 ; i < mag->size; ++i) {
  674. struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
  675. if (WARN_ON(!iova))
  676. continue;
  677. private_free_iova(iovad, iova);
  678. }
  679. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  680. mag->size = 0;
  681. }
  682. static bool iova_magazine_full(struct iova_magazine *mag)
  683. {
  684. return (mag && mag->size == IOVA_MAG_SIZE);
  685. }
  686. static bool iova_magazine_empty(struct iova_magazine *mag)
  687. {
  688. return (!mag || mag->size == 0);
  689. }
  690. static unsigned long iova_magazine_pop(struct iova_magazine *mag,
  691. unsigned long limit_pfn)
  692. {
  693. int i;
  694. unsigned long pfn;
  695. BUG_ON(iova_magazine_empty(mag));
  696. /* Only fall back to the rbtree if we have no suitable pfns at all */
  697. for (i = mag->size - 1; mag->pfns[i] > limit_pfn; i--)
  698. if (i == 0)
  699. return 0;
  700. /* Swap it to pop it */
  701. pfn = mag->pfns[i];
  702. mag->pfns[i] = mag->pfns[--mag->size];
  703. return pfn;
  704. }
  705. static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
  706. {
  707. BUG_ON(iova_magazine_full(mag));
  708. mag->pfns[mag->size++] = pfn;
  709. }
  710. static void init_iova_rcaches(struct iova_domain *iovad)
  711. {
  712. struct iova_cpu_rcache *cpu_rcache;
  713. struct iova_rcache *rcache;
  714. unsigned int cpu;
  715. int i;
  716. for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
  717. rcache = &iovad->rcaches[i];
  718. spin_lock_init(&rcache->lock);
  719. rcache->depot_size = 0;
  720. rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());
  721. if (WARN_ON(!rcache->cpu_rcaches))
  722. continue;
  723. for_each_possible_cpu(cpu) {
  724. cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
  725. spin_lock_init(&cpu_rcache->lock);
  726. cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
  727. cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
  728. }
  729. }
  730. }
  731. /*
  732. * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
  733. * return true on success. Can fail if rcache is full and we can't free
  734. * space, and free_iova() (our only caller) will then return the IOVA
  735. * range to the rbtree instead.
  736. */
  737. static bool __iova_rcache_insert(struct iova_domain *iovad,
  738. struct iova_rcache *rcache,
  739. unsigned long iova_pfn)
  740. {
  741. struct iova_magazine *mag_to_free = NULL;
  742. struct iova_cpu_rcache *cpu_rcache;
  743. bool can_insert = false;
  744. unsigned long flags;
  745. cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
  746. spin_lock_irqsave(&cpu_rcache->lock, flags);
  747. if (!iova_magazine_full(cpu_rcache->loaded)) {
  748. can_insert = true;
  749. } else if (!iova_magazine_full(cpu_rcache->prev)) {
  750. swap(cpu_rcache->prev, cpu_rcache->loaded);
  751. can_insert = true;
  752. } else {
  753. struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
  754. if (new_mag) {
  755. spin_lock(&rcache->lock);
  756. if (rcache->depot_size < MAX_GLOBAL_MAGS) {
  757. rcache->depot[rcache->depot_size++] =
  758. cpu_rcache->loaded;
  759. } else {
  760. mag_to_free = cpu_rcache->loaded;
  761. }
  762. spin_unlock(&rcache->lock);
  763. cpu_rcache->loaded = new_mag;
  764. can_insert = true;
  765. }
  766. }
  767. if (can_insert)
  768. iova_magazine_push(cpu_rcache->loaded, iova_pfn);
  769. spin_unlock_irqrestore(&cpu_rcache->lock, flags);
  770. if (mag_to_free) {
  771. iova_magazine_free_pfns(mag_to_free, iovad);
  772. iova_magazine_free(mag_to_free);
  773. }
  774. return can_insert;
  775. }
  776. static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
  777. unsigned long size)
  778. {
  779. unsigned int log_size = order_base_2(size);
  780. if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
  781. return false;
  782. return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
  783. }
  784. /*
  785. * Caller wants to allocate a new IOVA range from 'rcache'. If we can
  786. * satisfy the request, return a matching non-NULL range and remove
  787. * it from the 'rcache'.
  788. */
  789. static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
  790. unsigned long limit_pfn)
  791. {
  792. struct iova_cpu_rcache *cpu_rcache;
  793. unsigned long iova_pfn = 0;
  794. bool has_pfn = false;
  795. unsigned long flags;
  796. cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
  797. spin_lock_irqsave(&cpu_rcache->lock, flags);
  798. if (!iova_magazine_empty(cpu_rcache->loaded)) {
  799. has_pfn = true;
  800. } else if (!iova_magazine_empty(cpu_rcache->prev)) {
  801. swap(cpu_rcache->prev, cpu_rcache->loaded);
  802. has_pfn = true;
  803. } else {
  804. spin_lock(&rcache->lock);
  805. if (rcache->depot_size > 0) {
  806. iova_magazine_free(cpu_rcache->loaded);
  807. cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
  808. has_pfn = true;
  809. }
  810. spin_unlock(&rcache->lock);
  811. }
  812. if (has_pfn)
  813. iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
  814. spin_unlock_irqrestore(&cpu_rcache->lock, flags);
  815. return iova_pfn;
  816. }
  817. /*
  818. * Try to satisfy IOVA allocation range from rcache. Fail if requested
  819. * size is too big or the DMA limit we are given isn't satisfied by the
  820. * top element in the magazine.
  821. */
  822. static unsigned long iova_rcache_get(struct iova_domain *iovad,
  823. unsigned long size,
  824. unsigned long limit_pfn)
  825. {
  826. unsigned int log_size = order_base_2(size);
  827. if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
  828. return 0;
  829. return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn - size);
  830. }
  831. /*
  832. * free rcache data structures.
  833. */
  834. static void free_iova_rcaches(struct iova_domain *iovad)
  835. {
  836. struct iova_rcache *rcache;
  837. struct iova_cpu_rcache *cpu_rcache;
  838. unsigned int cpu;
  839. int i, j;
  840. for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
  841. rcache = &iovad->rcaches[i];
  842. for_each_possible_cpu(cpu) {
  843. cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
  844. iova_magazine_free(cpu_rcache->loaded);
  845. iova_magazine_free(cpu_rcache->prev);
  846. }
  847. free_percpu(rcache->cpu_rcaches);
  848. for (j = 0; j < rcache->depot_size; ++j)
  849. iova_magazine_free(rcache->depot[j]);
  850. }
  851. }
  852. /*
  853. * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
  854. */
  855. void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
  856. {
  857. struct iova_cpu_rcache *cpu_rcache;
  858. struct iova_rcache *rcache;
  859. unsigned long flags;
  860. int i;
  861. for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
  862. rcache = &iovad->rcaches[i];
  863. cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
  864. spin_lock_irqsave(&cpu_rcache->lock, flags);
  865. iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
  866. iova_magazine_free_pfns(cpu_rcache->prev, iovad);
  867. spin_unlock_irqrestore(&cpu_rcache->lock, flags);
  868. }
  869. }
  870. MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
  871. MODULE_LICENSE("GPL");