pgalloc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Page table allocation functions
  4. *
  5. * Copyright IBM Corp. 2016
  6. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  7. */
  8. #include <linux/sysctl.h>
  9. #include <linux/slab.h>
  10. #include <linux/mm.h>
  11. #include <asm/mmu_context.h>
  12. #include <asm/pgalloc.h>
  13. #include <asm/gmap.h>
  14. #include <asm/tlb.h>
  15. #include <asm/tlbflush.h>
  16. #ifdef CONFIG_PGSTE
  17. static int page_table_allocate_pgste_min = 0;
  18. static int page_table_allocate_pgste_max = 1;
  19. int page_table_allocate_pgste = 0;
  20. EXPORT_SYMBOL(page_table_allocate_pgste);
  21. static struct ctl_table page_table_sysctl[] = {
  22. {
  23. .procname = "allocate_pgste",
  24. .data = &page_table_allocate_pgste,
  25. .maxlen = sizeof(int),
  26. .mode = S_IRUGO | S_IWUSR,
  27. .proc_handler = proc_dointvec_minmax,
  28. .extra1 = &page_table_allocate_pgste_min,
  29. .extra2 = &page_table_allocate_pgste_max,
  30. },
  31. { }
  32. };
  33. static struct ctl_table page_table_sysctl_dir[] = {
  34. {
  35. .procname = "vm",
  36. .maxlen = 0,
  37. .mode = 0555,
  38. .child = page_table_sysctl,
  39. },
  40. { }
  41. };
  42. static int __init page_table_register_sysctl(void)
  43. {
  44. return register_sysctl_table(page_table_sysctl_dir) ? 0 : -ENOMEM;
  45. }
  46. __initcall(page_table_register_sysctl);
  47. #endif /* CONFIG_PGSTE */
  48. unsigned long *crst_table_alloc(struct mm_struct *mm)
  49. {
  50. struct page *page = alloc_pages(GFP_KERNEL, 2);
  51. if (!page)
  52. return NULL;
  53. arch_set_page_dat(page, 2);
  54. return (unsigned long *) page_to_phys(page);
  55. }
  56. void crst_table_free(struct mm_struct *mm, unsigned long *table)
  57. {
  58. free_pages((unsigned long) table, 2);
  59. }
  60. static void __crst_table_upgrade(void *arg)
  61. {
  62. struct mm_struct *mm = arg;
  63. /* we must change all active ASCEs to avoid the creation of new TLBs */
  64. if (current->active_mm == mm) {
  65. S390_lowcore.user_asce = mm->context.asce;
  66. if (current->thread.mm_segment == USER_DS) {
  67. __ctl_load(S390_lowcore.user_asce, 1, 1);
  68. /* Mark user-ASCE present in CR1 */
  69. clear_cpu_flag(CIF_ASCE_PRIMARY);
  70. }
  71. if (current->thread.mm_segment == USER_DS_SACF) {
  72. __ctl_load(S390_lowcore.user_asce, 7, 7);
  73. /* enable_sacf_uaccess does all or nothing */
  74. WARN_ON(!test_cpu_flag(CIF_ASCE_SECONDARY));
  75. }
  76. }
  77. __tlb_flush_local();
  78. }
  79. int crst_table_upgrade(struct mm_struct *mm, unsigned long end)
  80. {
  81. unsigned long *table, *pgd;
  82. int rc, notify;
  83. /* upgrade should only happen from 3 to 4, 3 to 5, or 4 to 5 levels */
  84. VM_BUG_ON(mm->context.asce_limit < _REGION2_SIZE);
  85. rc = 0;
  86. notify = 0;
  87. while (mm->context.asce_limit < end) {
  88. table = crst_table_alloc(mm);
  89. if (!table) {
  90. rc = -ENOMEM;
  91. break;
  92. }
  93. spin_lock_bh(&mm->page_table_lock);
  94. pgd = (unsigned long *) mm->pgd;
  95. if (mm->context.asce_limit == _REGION2_SIZE) {
  96. crst_table_init(table, _REGION2_ENTRY_EMPTY);
  97. p4d_populate(mm, (p4d_t *) table, (pud_t *) pgd);
  98. mm->pgd = (pgd_t *) table;
  99. mm->context.asce_limit = _REGION1_SIZE;
  100. mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
  101. _ASCE_USER_BITS | _ASCE_TYPE_REGION2;
  102. mm_inc_nr_puds(mm);
  103. } else {
  104. crst_table_init(table, _REGION1_ENTRY_EMPTY);
  105. pgd_populate(mm, (pgd_t *) table, (p4d_t *) pgd);
  106. mm->pgd = (pgd_t *) table;
  107. mm->context.asce_limit = -PAGE_SIZE;
  108. mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
  109. _ASCE_USER_BITS | _ASCE_TYPE_REGION1;
  110. }
  111. notify = 1;
  112. spin_unlock_bh(&mm->page_table_lock);
  113. }
  114. if (notify)
  115. on_each_cpu(__crst_table_upgrade, mm, 0);
  116. return rc;
  117. }
  118. void crst_table_downgrade(struct mm_struct *mm)
  119. {
  120. pgd_t *pgd;
  121. /* downgrade should only happen from 3 to 2 levels (compat only) */
  122. VM_BUG_ON(mm->context.asce_limit != _REGION2_SIZE);
  123. if (current->active_mm == mm) {
  124. clear_user_asce();
  125. __tlb_flush_mm(mm);
  126. }
  127. pgd = mm->pgd;
  128. mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
  129. mm->context.asce_limit = _REGION3_SIZE;
  130. mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
  131. _ASCE_USER_BITS | _ASCE_TYPE_SEGMENT;
  132. crst_table_free(mm, (unsigned long *) pgd);
  133. if (current->active_mm == mm)
  134. set_user_asce(mm);
  135. }
  136. static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
  137. {
  138. unsigned int old, new;
  139. do {
  140. old = atomic_read(v);
  141. new = old ^ bits;
  142. } while (atomic_cmpxchg(v, old, new) != old);
  143. return new;
  144. }
  145. #ifdef CONFIG_PGSTE
  146. struct page *page_table_alloc_pgste(struct mm_struct *mm)
  147. {
  148. struct page *page;
  149. u64 *table;
  150. page = alloc_page(GFP_KERNEL);
  151. if (page) {
  152. table = (u64 *)page_to_phys(page);
  153. memset64(table, _PAGE_INVALID, PTRS_PER_PTE);
  154. memset64(table + PTRS_PER_PTE, 0, PTRS_PER_PTE);
  155. }
  156. return page;
  157. }
  158. void page_table_free_pgste(struct page *page)
  159. {
  160. __free_page(page);
  161. }
  162. #endif /* CONFIG_PGSTE */
  163. /*
  164. * page table entry allocation/free routines.
  165. */
  166. unsigned long *page_table_alloc(struct mm_struct *mm)
  167. {
  168. unsigned long *table;
  169. struct page *page;
  170. unsigned int mask, bit;
  171. /* Try to get a fragment of a 4K page as a 2K page table */
  172. if (!mm_alloc_pgste(mm)) {
  173. table = NULL;
  174. spin_lock_bh(&mm->context.lock);
  175. if (!list_empty(&mm->context.pgtable_list)) {
  176. page = list_first_entry(&mm->context.pgtable_list,
  177. struct page, lru);
  178. mask = atomic_read(&page->_refcount) >> 24;
  179. mask = (mask | (mask >> 4)) & 3;
  180. if (mask != 3) {
  181. table = (unsigned long *) page_to_phys(page);
  182. bit = mask & 1; /* =1 -> second 2K */
  183. if (bit)
  184. table += PTRS_PER_PTE;
  185. atomic_xor_bits(&page->_refcount,
  186. 1U << (bit + 24));
  187. list_del(&page->lru);
  188. }
  189. }
  190. spin_unlock_bh(&mm->context.lock);
  191. if (table)
  192. return table;
  193. }
  194. /* Allocate a fresh page */
  195. page = alloc_page(GFP_KERNEL);
  196. if (!page)
  197. return NULL;
  198. if (!pgtable_page_ctor(page)) {
  199. __free_page(page);
  200. return NULL;
  201. }
  202. arch_set_page_dat(page, 0);
  203. /* Initialize page table */
  204. table = (unsigned long *) page_to_phys(page);
  205. if (mm_alloc_pgste(mm)) {
  206. /* Return 4K page table with PGSTEs */
  207. atomic_xor_bits(&page->_refcount, 3 << 24);
  208. memset64((u64 *)table, _PAGE_INVALID, PTRS_PER_PTE);
  209. memset64((u64 *)table + PTRS_PER_PTE, 0, PTRS_PER_PTE);
  210. } else {
  211. /* Return the first 2K fragment of the page */
  212. atomic_xor_bits(&page->_refcount, 1 << 24);
  213. memset64((u64 *)table, _PAGE_INVALID, 2 * PTRS_PER_PTE);
  214. spin_lock_bh(&mm->context.lock);
  215. list_add(&page->lru, &mm->context.pgtable_list);
  216. spin_unlock_bh(&mm->context.lock);
  217. }
  218. return table;
  219. }
  220. void page_table_free(struct mm_struct *mm, unsigned long *table)
  221. {
  222. struct page *page;
  223. unsigned int bit, mask;
  224. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  225. if (!mm_alloc_pgste(mm)) {
  226. /* Free 2K page table fragment of a 4K page */
  227. bit = (__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t));
  228. spin_lock_bh(&mm->context.lock);
  229. mask = atomic_xor_bits(&page->_refcount, 1U << (bit + 24));
  230. mask >>= 24;
  231. if (mask & 3)
  232. list_add(&page->lru, &mm->context.pgtable_list);
  233. else
  234. list_del(&page->lru);
  235. spin_unlock_bh(&mm->context.lock);
  236. if (mask != 0)
  237. return;
  238. } else {
  239. atomic_xor_bits(&page->_refcount, 3U << 24);
  240. }
  241. pgtable_page_dtor(page);
  242. __free_page(page);
  243. }
  244. void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table,
  245. unsigned long vmaddr)
  246. {
  247. struct mm_struct *mm;
  248. struct page *page;
  249. unsigned int bit, mask;
  250. mm = tlb->mm;
  251. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  252. if (mm_alloc_pgste(mm)) {
  253. gmap_unlink(mm, table, vmaddr);
  254. table = (unsigned long *) (__pa(table) | 3);
  255. tlb_remove_table(tlb, table);
  256. return;
  257. }
  258. bit = (__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t));
  259. spin_lock_bh(&mm->context.lock);
  260. mask = atomic_xor_bits(&page->_refcount, 0x11U << (bit + 24));
  261. mask >>= 24;
  262. if (mask & 3)
  263. list_add_tail(&page->lru, &mm->context.pgtable_list);
  264. else
  265. list_del(&page->lru);
  266. spin_unlock_bh(&mm->context.lock);
  267. table = (unsigned long *) (__pa(table) | (1U << bit));
  268. tlb_remove_table(tlb, table);
  269. }
  270. static void __tlb_remove_table(void *_table)
  271. {
  272. unsigned int mask = (unsigned long) _table & 3;
  273. void *table = (void *)((unsigned long) _table ^ mask);
  274. struct page *page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  275. switch (mask) {
  276. case 0: /* pmd, pud, or p4d */
  277. free_pages((unsigned long) table, 2);
  278. break;
  279. case 1: /* lower 2K of a 4K page table */
  280. case 2: /* higher 2K of a 4K page table */
  281. mask = atomic_xor_bits(&page->_refcount, mask << (4 + 24));
  282. mask >>= 24;
  283. if (mask != 0)
  284. break;
  285. /* fallthrough */
  286. case 3: /* 4K page table with pgstes */
  287. if (mask & 3)
  288. atomic_xor_bits(&page->_refcount, 3 << 24);
  289. pgtable_page_dtor(page);
  290. __free_page(page);
  291. break;
  292. }
  293. }
  294. static void tlb_remove_table_smp_sync(void *arg)
  295. {
  296. /* Simply deliver the interrupt */
  297. }
  298. static void tlb_remove_table_one(void *table)
  299. {
  300. /*
  301. * This isn't an RCU grace period and hence the page-tables cannot be
  302. * assumed to be actually RCU-freed.
  303. *
  304. * It is however sufficient for software page-table walkers that rely
  305. * on IRQ disabling. See the comment near struct mmu_table_batch.
  306. */
  307. smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
  308. __tlb_remove_table(table);
  309. }
  310. static void tlb_remove_table_rcu(struct rcu_head *head)
  311. {
  312. struct mmu_table_batch *batch;
  313. int i;
  314. batch = container_of(head, struct mmu_table_batch, rcu);
  315. for (i = 0; i < batch->nr; i++)
  316. __tlb_remove_table(batch->tables[i]);
  317. free_page((unsigned long)batch);
  318. }
  319. void tlb_table_flush(struct mmu_gather *tlb)
  320. {
  321. struct mmu_table_batch **batch = &tlb->batch;
  322. if (*batch) {
  323. call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
  324. *batch = NULL;
  325. }
  326. }
  327. void tlb_remove_table(struct mmu_gather *tlb, void *table)
  328. {
  329. struct mmu_table_batch **batch = &tlb->batch;
  330. tlb->mm->context.flush_mm = 1;
  331. if (*batch == NULL) {
  332. *batch = (struct mmu_table_batch *)
  333. __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
  334. if (*batch == NULL) {
  335. __tlb_flush_mm_lazy(tlb->mm);
  336. tlb_remove_table_one(table);
  337. return;
  338. }
  339. (*batch)->nr = 0;
  340. }
  341. (*batch)->tables[(*batch)->nr++] = table;
  342. if ((*batch)->nr == MAX_TABLE_BATCH)
  343. tlb_flush_mmu(tlb);
  344. }
  345. /*
  346. * Base infrastructure required to generate basic asces, region, segment,
  347. * and page tables that do not make use of enhanced features like EDAT1.
  348. */
  349. static struct kmem_cache *base_pgt_cache;
  350. static unsigned long base_pgt_alloc(void)
  351. {
  352. u64 *table;
  353. table = kmem_cache_alloc(base_pgt_cache, GFP_KERNEL);
  354. if (table)
  355. memset64(table, _PAGE_INVALID, PTRS_PER_PTE);
  356. return (unsigned long) table;
  357. }
  358. static void base_pgt_free(unsigned long table)
  359. {
  360. kmem_cache_free(base_pgt_cache, (void *) table);
  361. }
  362. static unsigned long base_crst_alloc(unsigned long val)
  363. {
  364. unsigned long table;
  365. table = __get_free_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
  366. if (table)
  367. crst_table_init((unsigned long *)table, val);
  368. return table;
  369. }
  370. static void base_crst_free(unsigned long table)
  371. {
  372. free_pages(table, CRST_ALLOC_ORDER);
  373. }
  374. #define BASE_ADDR_END_FUNC(NAME, SIZE) \
  375. static inline unsigned long base_##NAME##_addr_end(unsigned long addr, \
  376. unsigned long end) \
  377. { \
  378. unsigned long next = (addr + (SIZE)) & ~((SIZE) - 1); \
  379. \
  380. return (next - 1) < (end - 1) ? next : end; \
  381. }
  382. BASE_ADDR_END_FUNC(page, _PAGE_SIZE)
  383. BASE_ADDR_END_FUNC(segment, _SEGMENT_SIZE)
  384. BASE_ADDR_END_FUNC(region3, _REGION3_SIZE)
  385. BASE_ADDR_END_FUNC(region2, _REGION2_SIZE)
  386. BASE_ADDR_END_FUNC(region1, _REGION1_SIZE)
  387. static inline unsigned long base_lra(unsigned long address)
  388. {
  389. unsigned long real;
  390. asm volatile(
  391. " lra %0,0(%1)\n"
  392. : "=d" (real) : "a" (address) : "cc");
  393. return real;
  394. }
  395. static int base_page_walk(unsigned long origin, unsigned long addr,
  396. unsigned long end, int alloc)
  397. {
  398. unsigned long *pte, next;
  399. if (!alloc)
  400. return 0;
  401. pte = (unsigned long *) origin;
  402. pte += (addr & _PAGE_INDEX) >> _PAGE_SHIFT;
  403. do {
  404. next = base_page_addr_end(addr, end);
  405. *pte = base_lra(addr);
  406. } while (pte++, addr = next, addr < end);
  407. return 0;
  408. }
  409. static int base_segment_walk(unsigned long origin, unsigned long addr,
  410. unsigned long end, int alloc)
  411. {
  412. unsigned long *ste, next, table;
  413. int rc;
  414. ste = (unsigned long *) origin;
  415. ste += (addr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
  416. do {
  417. next = base_segment_addr_end(addr, end);
  418. if (*ste & _SEGMENT_ENTRY_INVALID) {
  419. if (!alloc)
  420. continue;
  421. table = base_pgt_alloc();
  422. if (!table)
  423. return -ENOMEM;
  424. *ste = table | _SEGMENT_ENTRY;
  425. }
  426. table = *ste & _SEGMENT_ENTRY_ORIGIN;
  427. rc = base_page_walk(table, addr, next, alloc);
  428. if (rc)
  429. return rc;
  430. if (!alloc)
  431. base_pgt_free(table);
  432. cond_resched();
  433. } while (ste++, addr = next, addr < end);
  434. return 0;
  435. }
  436. static int base_region3_walk(unsigned long origin, unsigned long addr,
  437. unsigned long end, int alloc)
  438. {
  439. unsigned long *rtte, next, table;
  440. int rc;
  441. rtte = (unsigned long *) origin;
  442. rtte += (addr & _REGION3_INDEX) >> _REGION3_SHIFT;
  443. do {
  444. next = base_region3_addr_end(addr, end);
  445. if (*rtte & _REGION_ENTRY_INVALID) {
  446. if (!alloc)
  447. continue;
  448. table = base_crst_alloc(_SEGMENT_ENTRY_EMPTY);
  449. if (!table)
  450. return -ENOMEM;
  451. *rtte = table | _REGION3_ENTRY;
  452. }
  453. table = *rtte & _REGION_ENTRY_ORIGIN;
  454. rc = base_segment_walk(table, addr, next, alloc);
  455. if (rc)
  456. return rc;
  457. if (!alloc)
  458. base_crst_free(table);
  459. } while (rtte++, addr = next, addr < end);
  460. return 0;
  461. }
  462. static int base_region2_walk(unsigned long origin, unsigned long addr,
  463. unsigned long end, int alloc)
  464. {
  465. unsigned long *rste, next, table;
  466. int rc;
  467. rste = (unsigned long *) origin;
  468. rste += (addr & _REGION2_INDEX) >> _REGION2_SHIFT;
  469. do {
  470. next = base_region2_addr_end(addr, end);
  471. if (*rste & _REGION_ENTRY_INVALID) {
  472. if (!alloc)
  473. continue;
  474. table = base_crst_alloc(_REGION3_ENTRY_EMPTY);
  475. if (!table)
  476. return -ENOMEM;
  477. *rste = table | _REGION2_ENTRY;
  478. }
  479. table = *rste & _REGION_ENTRY_ORIGIN;
  480. rc = base_region3_walk(table, addr, next, alloc);
  481. if (rc)
  482. return rc;
  483. if (!alloc)
  484. base_crst_free(table);
  485. } while (rste++, addr = next, addr < end);
  486. return 0;
  487. }
  488. static int base_region1_walk(unsigned long origin, unsigned long addr,
  489. unsigned long end, int alloc)
  490. {
  491. unsigned long *rfte, next, table;
  492. int rc;
  493. rfte = (unsigned long *) origin;
  494. rfte += (addr & _REGION1_INDEX) >> _REGION1_SHIFT;
  495. do {
  496. next = base_region1_addr_end(addr, end);
  497. if (*rfte & _REGION_ENTRY_INVALID) {
  498. if (!alloc)
  499. continue;
  500. table = base_crst_alloc(_REGION2_ENTRY_EMPTY);
  501. if (!table)
  502. return -ENOMEM;
  503. *rfte = table | _REGION1_ENTRY;
  504. }
  505. table = *rfte & _REGION_ENTRY_ORIGIN;
  506. rc = base_region2_walk(table, addr, next, alloc);
  507. if (rc)
  508. return rc;
  509. if (!alloc)
  510. base_crst_free(table);
  511. } while (rfte++, addr = next, addr < end);
  512. return 0;
  513. }
  514. /**
  515. * base_asce_free - free asce and tables returned from base_asce_alloc()
  516. * @asce: asce to be freed
  517. *
  518. * Frees all region, segment, and page tables that were allocated with a
  519. * corresponding base_asce_alloc() call.
  520. */
  521. void base_asce_free(unsigned long asce)
  522. {
  523. unsigned long table = asce & _ASCE_ORIGIN;
  524. if (!asce)
  525. return;
  526. switch (asce & _ASCE_TYPE_MASK) {
  527. case _ASCE_TYPE_SEGMENT:
  528. base_segment_walk(table, 0, _REGION3_SIZE, 0);
  529. break;
  530. case _ASCE_TYPE_REGION3:
  531. base_region3_walk(table, 0, _REGION2_SIZE, 0);
  532. break;
  533. case _ASCE_TYPE_REGION2:
  534. base_region2_walk(table, 0, _REGION1_SIZE, 0);
  535. break;
  536. case _ASCE_TYPE_REGION1:
  537. base_region1_walk(table, 0, -_PAGE_SIZE, 0);
  538. break;
  539. }
  540. base_crst_free(table);
  541. }
  542. static int base_pgt_cache_init(void)
  543. {
  544. static DEFINE_MUTEX(base_pgt_cache_mutex);
  545. unsigned long sz = _PAGE_TABLE_SIZE;
  546. if (base_pgt_cache)
  547. return 0;
  548. mutex_lock(&base_pgt_cache_mutex);
  549. if (!base_pgt_cache)
  550. base_pgt_cache = kmem_cache_create("base_pgt", sz, sz, 0, NULL);
  551. mutex_unlock(&base_pgt_cache_mutex);
  552. return base_pgt_cache ? 0 : -ENOMEM;
  553. }
  554. /**
  555. * base_asce_alloc - create kernel mapping without enhanced DAT features
  556. * @addr: virtual start address of kernel mapping
  557. * @num_pages: number of consecutive pages
  558. *
  559. * Generate an asce, including all required region, segment and page tables,
  560. * that can be used to access the virtual kernel mapping. The difference is
  561. * that the returned asce does not make use of any enhanced DAT features like
  562. * e.g. large pages. This is required for some I/O functions that pass an
  563. * asce, like e.g. some service call requests.
  564. *
  565. * Note: the returned asce may NEVER be attached to any cpu. It may only be
  566. * used for I/O requests. tlb entries that might result because the
  567. * asce was attached to a cpu won't be cleared.
  568. */
  569. unsigned long base_asce_alloc(unsigned long addr, unsigned long num_pages)
  570. {
  571. unsigned long asce, table, end;
  572. int rc;
  573. if (base_pgt_cache_init())
  574. return 0;
  575. end = addr + num_pages * PAGE_SIZE;
  576. if (end <= _REGION3_SIZE) {
  577. table = base_crst_alloc(_SEGMENT_ENTRY_EMPTY);
  578. if (!table)
  579. return 0;
  580. rc = base_segment_walk(table, addr, end, 1);
  581. asce = table | _ASCE_TYPE_SEGMENT | _ASCE_TABLE_LENGTH;
  582. } else if (end <= _REGION2_SIZE) {
  583. table = base_crst_alloc(_REGION3_ENTRY_EMPTY);
  584. if (!table)
  585. return 0;
  586. rc = base_region3_walk(table, addr, end, 1);
  587. asce = table | _ASCE_TYPE_REGION3 | _ASCE_TABLE_LENGTH;
  588. } else if (end <= _REGION1_SIZE) {
  589. table = base_crst_alloc(_REGION2_ENTRY_EMPTY);
  590. if (!table)
  591. return 0;
  592. rc = base_region2_walk(table, addr, end, 1);
  593. asce = table | _ASCE_TYPE_REGION2 | _ASCE_TABLE_LENGTH;
  594. } else {
  595. table = base_crst_alloc(_REGION1_ENTRY_EMPTY);
  596. if (!table)
  597. return 0;
  598. rc = base_region1_walk(table, addr, end, 1);
  599. asce = table | _ASCE_TYPE_REGION1 | _ASCE_TABLE_LENGTH;
  600. }
  601. if (rc) {
  602. base_asce_free(asce);
  603. asce = 0;
  604. }
  605. return asce;
  606. }