pgalloc.c 16 KB

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