pgalloc.c 17 KB

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