iova.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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. BUG_ON(!iova);
  676. private_free_iova(iovad, iova);
  677. }
  678. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  679. mag->size = 0;
  680. }
  681. static bool iova_magazine_full(struct iova_magazine *mag)
  682. {
  683. return (mag && mag->size == IOVA_MAG_SIZE);
  684. }
  685. static bool iova_magazine_empty(struct iova_magazine *mag)
  686. {
  687. return (!mag || mag->size == 0);
  688. }
  689. static unsigned long iova_magazine_pop(struct iova_magazine *mag,
  690. unsigned long limit_pfn)
  691. {
  692. int i;
  693. unsigned long pfn;
  694. BUG_ON(iova_magazine_empty(mag));
  695. /* Only fall back to the rbtree if we have no suitable pfns at all */
  696. for (i = mag->size - 1; mag->pfns[i] > limit_pfn; i--)
  697. if (i == 0)
  698. return 0;
  699. /* Swap it to pop it */
  700. pfn = mag->pfns[i];
  701. mag->pfns[i] = mag->pfns[--mag->size];
  702. return pfn;
  703. }
  704. static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
  705. {
  706. BUG_ON(iova_magazine_full(mag));
  707. mag->pfns[mag->size++] = pfn;
  708. }
  709. static void init_iova_rcaches(struct iova_domain *iovad)
  710. {
  711. struct iova_cpu_rcache *cpu_rcache;
  712. struct iova_rcache *rcache;
  713. unsigned int cpu;
  714. int i;
  715. for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
  716. rcache = &iovad->rcaches[i];
  717. spin_lock_init(&rcache->lock);
  718. rcache->depot_size = 0;
  719. rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());
  720. if (WARN_ON(!rcache->cpu_rcaches))
  721. continue;
  722. for_each_possible_cpu(cpu) {
  723. cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
  724. spin_lock_init(&cpu_rcache->lock);
  725. cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
  726. cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
  727. }
  728. }
  729. }
  730. /*
  731. * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
  732. * return true on success. Can fail if rcache is full and we can't free
  733. * space, and free_iova() (our only caller) will then return the IOVA
  734. * range to the rbtree instead.
  735. */
  736. static bool __iova_rcache_insert(struct iova_domain *iovad,
  737. struct iova_rcache *rcache,
  738. unsigned long iova_pfn)
  739. {
  740. struct iova_magazine *mag_to_free = NULL;
  741. struct iova_cpu_rcache *cpu_rcache;
  742. bool can_insert = false;
  743. unsigned long flags;
  744. cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
  745. spin_lock_irqsave(&cpu_rcache->lock, flags);
  746. if (!iova_magazine_full(cpu_rcache->loaded)) {
  747. can_insert = true;
  748. } else if (!iova_magazine_full(cpu_rcache->prev)) {
  749. swap(cpu_rcache->prev, cpu_rcache->loaded);
  750. can_insert = true;
  751. } else {
  752. struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
  753. if (new_mag) {
  754. spin_lock(&rcache->lock);
  755. if (rcache->depot_size < MAX_GLOBAL_MAGS) {
  756. rcache->depot[rcache->depot_size++] =
  757. cpu_rcache->loaded;
  758. } else {
  759. mag_to_free = cpu_rcache->loaded;
  760. }
  761. spin_unlock(&rcache->lock);
  762. cpu_rcache->loaded = new_mag;
  763. can_insert = true;
  764. }
  765. }
  766. if (can_insert)
  767. iova_magazine_push(cpu_rcache->loaded, iova_pfn);
  768. spin_unlock_irqrestore(&cpu_rcache->lock, flags);
  769. if (mag_to_free) {
  770. iova_magazine_free_pfns(mag_to_free, iovad);
  771. iova_magazine_free(mag_to_free);
  772. }
  773. return can_insert;
  774. }
  775. static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
  776. unsigned long size)
  777. {
  778. unsigned int log_size = order_base_2(size);
  779. if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
  780. return false;
  781. return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
  782. }
  783. /*
  784. * Caller wants to allocate a new IOVA range from 'rcache'. If we can
  785. * satisfy the request, return a matching non-NULL range and remove
  786. * it from the 'rcache'.
  787. */
  788. static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
  789. unsigned long limit_pfn)
  790. {
  791. struct iova_cpu_rcache *cpu_rcache;
  792. unsigned long iova_pfn = 0;
  793. bool has_pfn = false;
  794. unsigned long flags;
  795. cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
  796. spin_lock_irqsave(&cpu_rcache->lock, flags);
  797. if (!iova_magazine_empty(cpu_rcache->loaded)) {
  798. has_pfn = true;
  799. } else if (!iova_magazine_empty(cpu_rcache->prev)) {
  800. swap(cpu_rcache->prev, cpu_rcache->loaded);
  801. has_pfn = true;
  802. } else {
  803. spin_lock(&rcache->lock);
  804. if (rcache->depot_size > 0) {
  805. iova_magazine_free(cpu_rcache->loaded);
  806. cpu_rcache->loaded = rcache->depot[--rcache->depot_size];
  807. has_pfn = true;
  808. }
  809. spin_unlock(&rcache->lock);
  810. }
  811. if (has_pfn)
  812. iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
  813. spin_unlock_irqrestore(&cpu_rcache->lock, flags);
  814. return iova_pfn;
  815. }
  816. /*
  817. * Try to satisfy IOVA allocation range from rcache. Fail if requested
  818. * size is too big or the DMA limit we are given isn't satisfied by the
  819. * top element in the magazine.
  820. */
  821. static unsigned long iova_rcache_get(struct iova_domain *iovad,
  822. unsigned long size,
  823. unsigned long limit_pfn)
  824. {
  825. unsigned int log_size = order_base_2(size);
  826. if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
  827. return 0;
  828. return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn - size);
  829. }
  830. /*
  831. * free rcache data structures.
  832. */
  833. static void free_iova_rcaches(struct iova_domain *iovad)
  834. {
  835. struct iova_rcache *rcache;
  836. struct iova_cpu_rcache *cpu_rcache;
  837. unsigned int cpu;
  838. int i, j;
  839. for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
  840. rcache = &iovad->rcaches[i];
  841. for_each_possible_cpu(cpu) {
  842. cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
  843. iova_magazine_free(cpu_rcache->loaded);
  844. iova_magazine_free(cpu_rcache->prev);
  845. }
  846. free_percpu(rcache->cpu_rcaches);
  847. for (j = 0; j < rcache->depot_size; ++j)
  848. iova_magazine_free(rcache->depot[j]);
  849. }
  850. }
  851. /*
  852. * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
  853. */
  854. void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
  855. {
  856. struct iova_cpu_rcache *cpu_rcache;
  857. struct iova_rcache *rcache;
  858. unsigned long flags;
  859. int i;
  860. for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
  861. rcache = &iovad->rcaches[i];
  862. cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
  863. spin_lock_irqsave(&cpu_rcache->lock, flags);
  864. iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
  865. iova_magazine_free_pfns(cpu_rcache->prev, iovad);
  866. spin_unlock_irqrestore(&cpu_rcache->lock, flags);
  867. }
  868. }
  869. MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
  870. MODULE_LICENSE("GPL");