ttm_memory.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /**************************************************************************
  3. *
  4. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  25. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. **************************************************************************/
  28. #define pr_fmt(fmt) "[TTM] " fmt
  29. #include <drm/ttm/ttm_memory.h>
  30. #include <drm/ttm/ttm_module.h>
  31. #include <drm/ttm/ttm_page_alloc.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/sched.h>
  34. #include <linux/wait.h>
  35. #include <linux/mm.h>
  36. #include <linux/module.h>
  37. #include <linux/slab.h>
  38. #include <linux/swap.h>
  39. #define TTM_MEMORY_ALLOC_RETRIES 4
  40. struct ttm_mem_global ttm_mem_glob;
  41. EXPORT_SYMBOL(ttm_mem_glob);
  42. struct ttm_mem_zone {
  43. struct kobject kobj;
  44. struct ttm_mem_global *glob;
  45. const char *name;
  46. uint64_t zone_mem;
  47. uint64_t emer_mem;
  48. uint64_t max_mem;
  49. uint64_t swap_limit;
  50. uint64_t used_mem;
  51. };
  52. static struct attribute ttm_mem_sys = {
  53. .name = "zone_memory",
  54. .mode = S_IRUGO
  55. };
  56. static struct attribute ttm_mem_emer = {
  57. .name = "emergency_memory",
  58. .mode = S_IRUGO | S_IWUSR
  59. };
  60. static struct attribute ttm_mem_max = {
  61. .name = "available_memory",
  62. .mode = S_IRUGO | S_IWUSR
  63. };
  64. static struct attribute ttm_mem_swap = {
  65. .name = "swap_limit",
  66. .mode = S_IRUGO | S_IWUSR
  67. };
  68. static struct attribute ttm_mem_used = {
  69. .name = "used_memory",
  70. .mode = S_IRUGO
  71. };
  72. static void ttm_mem_zone_kobj_release(struct kobject *kobj)
  73. {
  74. struct ttm_mem_zone *zone =
  75. container_of(kobj, struct ttm_mem_zone, kobj);
  76. pr_info("Zone %7s: Used memory at exit: %llu KiB\n",
  77. zone->name, (unsigned long long)zone->used_mem >> 10);
  78. kfree(zone);
  79. }
  80. static ssize_t ttm_mem_zone_show(struct kobject *kobj,
  81. struct attribute *attr,
  82. char *buffer)
  83. {
  84. struct ttm_mem_zone *zone =
  85. container_of(kobj, struct ttm_mem_zone, kobj);
  86. uint64_t val = 0;
  87. spin_lock(&zone->glob->lock);
  88. if (attr == &ttm_mem_sys)
  89. val = zone->zone_mem;
  90. else if (attr == &ttm_mem_emer)
  91. val = zone->emer_mem;
  92. else if (attr == &ttm_mem_max)
  93. val = zone->max_mem;
  94. else if (attr == &ttm_mem_swap)
  95. val = zone->swap_limit;
  96. else if (attr == &ttm_mem_used)
  97. val = zone->used_mem;
  98. spin_unlock(&zone->glob->lock);
  99. return snprintf(buffer, PAGE_SIZE, "%llu\n",
  100. (unsigned long long) val >> 10);
  101. }
  102. static void ttm_check_swapping(struct ttm_mem_global *glob);
  103. static ssize_t ttm_mem_zone_store(struct kobject *kobj,
  104. struct attribute *attr,
  105. const char *buffer,
  106. size_t size)
  107. {
  108. struct ttm_mem_zone *zone =
  109. container_of(kobj, struct ttm_mem_zone, kobj);
  110. int chars;
  111. unsigned long val;
  112. uint64_t val64;
  113. chars = sscanf(buffer, "%lu", &val);
  114. if (chars == 0)
  115. return size;
  116. val64 = val;
  117. val64 <<= 10;
  118. spin_lock(&zone->glob->lock);
  119. if (val64 > zone->zone_mem)
  120. val64 = zone->zone_mem;
  121. if (attr == &ttm_mem_emer) {
  122. zone->emer_mem = val64;
  123. if (zone->max_mem > val64)
  124. zone->max_mem = val64;
  125. } else if (attr == &ttm_mem_max) {
  126. zone->max_mem = val64;
  127. if (zone->emer_mem < val64)
  128. zone->emer_mem = val64;
  129. } else if (attr == &ttm_mem_swap)
  130. zone->swap_limit = val64;
  131. spin_unlock(&zone->glob->lock);
  132. ttm_check_swapping(zone->glob);
  133. return size;
  134. }
  135. static struct attribute *ttm_mem_zone_attrs[] = {
  136. &ttm_mem_sys,
  137. &ttm_mem_emer,
  138. &ttm_mem_max,
  139. &ttm_mem_swap,
  140. &ttm_mem_used,
  141. NULL
  142. };
  143. static const struct sysfs_ops ttm_mem_zone_ops = {
  144. .show = &ttm_mem_zone_show,
  145. .store = &ttm_mem_zone_store
  146. };
  147. static struct kobj_type ttm_mem_zone_kobj_type = {
  148. .release = &ttm_mem_zone_kobj_release,
  149. .sysfs_ops = &ttm_mem_zone_ops,
  150. .default_attrs = ttm_mem_zone_attrs,
  151. };
  152. static struct attribute ttm_mem_global_lower_mem_limit = {
  153. .name = "lower_mem_limit",
  154. .mode = S_IRUGO | S_IWUSR
  155. };
  156. static ssize_t ttm_mem_global_show(struct kobject *kobj,
  157. struct attribute *attr,
  158. char *buffer)
  159. {
  160. struct ttm_mem_global *glob =
  161. container_of(kobj, struct ttm_mem_global, kobj);
  162. uint64_t val = 0;
  163. spin_lock(&glob->lock);
  164. val = glob->lower_mem_limit;
  165. spin_unlock(&glob->lock);
  166. /* convert from number of pages to KB */
  167. val <<= (PAGE_SHIFT - 10);
  168. return snprintf(buffer, PAGE_SIZE, "%llu\n",
  169. (unsigned long long) val);
  170. }
  171. static ssize_t ttm_mem_global_store(struct kobject *kobj,
  172. struct attribute *attr,
  173. const char *buffer,
  174. size_t size)
  175. {
  176. int chars;
  177. uint64_t val64;
  178. unsigned long val;
  179. struct ttm_mem_global *glob =
  180. container_of(kobj, struct ttm_mem_global, kobj);
  181. chars = sscanf(buffer, "%lu", &val);
  182. if (chars == 0)
  183. return size;
  184. val64 = val;
  185. /* convert from KB to number of pages */
  186. val64 >>= (PAGE_SHIFT - 10);
  187. spin_lock(&glob->lock);
  188. glob->lower_mem_limit = val64;
  189. spin_unlock(&glob->lock);
  190. return size;
  191. }
  192. static struct attribute *ttm_mem_global_attrs[] = {
  193. &ttm_mem_global_lower_mem_limit,
  194. NULL
  195. };
  196. static const struct sysfs_ops ttm_mem_global_ops = {
  197. .show = &ttm_mem_global_show,
  198. .store = &ttm_mem_global_store,
  199. };
  200. static struct kobj_type ttm_mem_glob_kobj_type = {
  201. .sysfs_ops = &ttm_mem_global_ops,
  202. .default_attrs = ttm_mem_global_attrs,
  203. };
  204. static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob,
  205. bool from_wq, uint64_t extra)
  206. {
  207. unsigned int i;
  208. struct ttm_mem_zone *zone;
  209. uint64_t target;
  210. for (i = 0; i < glob->num_zones; ++i) {
  211. zone = glob->zones[i];
  212. if (from_wq)
  213. target = zone->swap_limit;
  214. else if (capable(CAP_SYS_ADMIN))
  215. target = zone->emer_mem;
  216. else
  217. target = zone->max_mem;
  218. target = (extra > target) ? 0ULL : target;
  219. if (zone->used_mem > target)
  220. return true;
  221. }
  222. return false;
  223. }
  224. /*
  225. * At this point we only support a single shrink callback.
  226. * Extend this if needed, perhaps using a linked list of callbacks.
  227. * Note that this function is reentrant:
  228. * many threads may try to swap out at any given time.
  229. */
  230. static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq,
  231. uint64_t extra, struct ttm_operation_ctx *ctx)
  232. {
  233. int ret;
  234. spin_lock(&glob->lock);
  235. while (ttm_zones_above_swap_target(glob, from_wq, extra)) {
  236. spin_unlock(&glob->lock);
  237. ret = ttm_bo_swapout(&ttm_bo_glob, ctx);
  238. spin_lock(&glob->lock);
  239. if (unlikely(ret != 0))
  240. break;
  241. }
  242. spin_unlock(&glob->lock);
  243. }
  244. static void ttm_shrink_work(struct work_struct *work)
  245. {
  246. struct ttm_operation_ctx ctx = {
  247. .interruptible = false,
  248. .no_wait_gpu = false
  249. };
  250. struct ttm_mem_global *glob =
  251. container_of(work, struct ttm_mem_global, work);
  252. ttm_shrink(glob, true, 0ULL, &ctx);
  253. }
  254. static int ttm_mem_init_kernel_zone(struct ttm_mem_global *glob,
  255. const struct sysinfo *si)
  256. {
  257. struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
  258. uint64_t mem;
  259. int ret;
  260. if (unlikely(!zone))
  261. return -ENOMEM;
  262. mem = si->totalram - si->totalhigh;
  263. mem *= si->mem_unit;
  264. zone->name = "kernel";
  265. zone->zone_mem = mem;
  266. zone->max_mem = mem >> 1;
  267. zone->emer_mem = (mem >> 1) + (mem >> 2);
  268. zone->swap_limit = zone->max_mem - (mem >> 3);
  269. zone->used_mem = 0;
  270. zone->glob = glob;
  271. glob->zone_kernel = zone;
  272. ret = kobject_init_and_add(
  273. &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
  274. if (unlikely(ret != 0)) {
  275. kobject_put(&zone->kobj);
  276. return ret;
  277. }
  278. glob->zones[glob->num_zones++] = zone;
  279. return 0;
  280. }
  281. #ifdef CONFIG_HIGHMEM
  282. static int ttm_mem_init_highmem_zone(struct ttm_mem_global *glob,
  283. const struct sysinfo *si)
  284. {
  285. struct ttm_mem_zone *zone;
  286. uint64_t mem;
  287. int ret;
  288. if (si->totalhigh == 0)
  289. return 0;
  290. zone = kzalloc(sizeof(*zone), GFP_KERNEL);
  291. if (unlikely(!zone))
  292. return -ENOMEM;
  293. mem = si->totalram;
  294. mem *= si->mem_unit;
  295. zone->name = "highmem";
  296. zone->zone_mem = mem;
  297. zone->max_mem = mem >> 1;
  298. zone->emer_mem = (mem >> 1) + (mem >> 2);
  299. zone->swap_limit = zone->max_mem - (mem >> 3);
  300. zone->used_mem = 0;
  301. zone->glob = glob;
  302. glob->zone_highmem = zone;
  303. ret = kobject_init_and_add(
  304. &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, "%s",
  305. zone->name);
  306. if (unlikely(ret != 0)) {
  307. kobject_put(&zone->kobj);
  308. return ret;
  309. }
  310. glob->zones[glob->num_zones++] = zone;
  311. return 0;
  312. }
  313. #else
  314. static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
  315. const struct sysinfo *si)
  316. {
  317. struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
  318. uint64_t mem;
  319. int ret;
  320. if (unlikely(!zone))
  321. return -ENOMEM;
  322. mem = si->totalram;
  323. mem *= si->mem_unit;
  324. /**
  325. * No special dma32 zone needed.
  326. */
  327. if (mem <= ((uint64_t) 1ULL << 32)) {
  328. kfree(zone);
  329. return 0;
  330. }
  331. /*
  332. * Limit max dma32 memory to 4GB for now
  333. * until we can figure out how big this
  334. * zone really is.
  335. */
  336. mem = ((uint64_t) 1ULL << 32);
  337. zone->name = "dma32";
  338. zone->zone_mem = mem;
  339. zone->max_mem = mem >> 1;
  340. zone->emer_mem = (mem >> 1) + (mem >> 2);
  341. zone->swap_limit = zone->max_mem - (mem >> 3);
  342. zone->used_mem = 0;
  343. zone->glob = glob;
  344. glob->zone_dma32 = zone;
  345. ret = kobject_init_and_add(
  346. &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
  347. if (unlikely(ret != 0)) {
  348. kobject_put(&zone->kobj);
  349. return ret;
  350. }
  351. glob->zones[glob->num_zones++] = zone;
  352. return 0;
  353. }
  354. #endif
  355. int ttm_mem_global_init(struct ttm_mem_global *glob)
  356. {
  357. struct sysinfo si;
  358. int ret;
  359. int i;
  360. struct ttm_mem_zone *zone;
  361. spin_lock_init(&glob->lock);
  362. glob->swap_queue = create_singlethread_workqueue("ttm_swap");
  363. INIT_WORK(&glob->work, ttm_shrink_work);
  364. ret = kobject_init_and_add(
  365. &glob->kobj, &ttm_mem_glob_kobj_type, ttm_get_kobj(), "memory_accounting");
  366. if (unlikely(ret != 0)) {
  367. kobject_put(&glob->kobj);
  368. return ret;
  369. }
  370. si_meminfo(&si);
  371. /* set it as 0 by default to keep original behavior of OOM */
  372. glob->lower_mem_limit = 0;
  373. ret = ttm_mem_init_kernel_zone(glob, &si);
  374. if (unlikely(ret != 0))
  375. goto out_no_zone;
  376. #ifdef CONFIG_HIGHMEM
  377. ret = ttm_mem_init_highmem_zone(glob, &si);
  378. if (unlikely(ret != 0))
  379. goto out_no_zone;
  380. #else
  381. ret = ttm_mem_init_dma32_zone(glob, &si);
  382. if (unlikely(ret != 0))
  383. goto out_no_zone;
  384. #endif
  385. for (i = 0; i < glob->num_zones; ++i) {
  386. zone = glob->zones[i];
  387. pr_info("Zone %7s: Available graphics memory: %llu KiB\n",
  388. zone->name, (unsigned long long)zone->max_mem >> 10);
  389. }
  390. ttm_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE));
  391. ttm_dma_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE));
  392. return 0;
  393. out_no_zone:
  394. ttm_mem_global_release(glob);
  395. return ret;
  396. }
  397. void ttm_mem_global_release(struct ttm_mem_global *glob)
  398. {
  399. struct ttm_mem_zone *zone;
  400. unsigned int i;
  401. /* let the page allocator first stop the shrink work. */
  402. ttm_page_alloc_fini();
  403. ttm_dma_page_alloc_fini();
  404. flush_workqueue(glob->swap_queue);
  405. destroy_workqueue(glob->swap_queue);
  406. glob->swap_queue = NULL;
  407. for (i = 0; i < glob->num_zones; ++i) {
  408. zone = glob->zones[i];
  409. kobject_del(&zone->kobj);
  410. kobject_put(&zone->kobj);
  411. }
  412. kobject_del(&glob->kobj);
  413. kobject_put(&glob->kobj);
  414. memset(glob, 0, sizeof(*glob));
  415. }
  416. static void ttm_check_swapping(struct ttm_mem_global *glob)
  417. {
  418. bool needs_swapping = false;
  419. unsigned int i;
  420. struct ttm_mem_zone *zone;
  421. spin_lock(&glob->lock);
  422. for (i = 0; i < glob->num_zones; ++i) {
  423. zone = glob->zones[i];
  424. if (zone->used_mem > zone->swap_limit) {
  425. needs_swapping = true;
  426. break;
  427. }
  428. }
  429. spin_unlock(&glob->lock);
  430. if (unlikely(needs_swapping))
  431. (void)queue_work(glob->swap_queue, &glob->work);
  432. }
  433. static void ttm_mem_global_free_zone(struct ttm_mem_global *glob,
  434. struct ttm_mem_zone *single_zone,
  435. uint64_t amount)
  436. {
  437. unsigned int i;
  438. struct ttm_mem_zone *zone;
  439. spin_lock(&glob->lock);
  440. for (i = 0; i < glob->num_zones; ++i) {
  441. zone = glob->zones[i];
  442. if (single_zone && zone != single_zone)
  443. continue;
  444. zone->used_mem -= amount;
  445. }
  446. spin_unlock(&glob->lock);
  447. }
  448. void ttm_mem_global_free(struct ttm_mem_global *glob,
  449. uint64_t amount)
  450. {
  451. return ttm_mem_global_free_zone(glob, glob->zone_kernel, amount);
  452. }
  453. EXPORT_SYMBOL(ttm_mem_global_free);
  454. /*
  455. * check if the available mem is under lower memory limit
  456. *
  457. * a. if no swap disk at all or free swap space is under swap_mem_limit
  458. * but available system mem is bigger than sys_mem_limit, allow TTM
  459. * allocation;
  460. *
  461. * b. if the available system mem is less than sys_mem_limit but free
  462. * swap disk is bigger than swap_mem_limit, allow TTM allocation.
  463. */
  464. bool
  465. ttm_check_under_lowerlimit(struct ttm_mem_global *glob,
  466. uint64_t num_pages,
  467. struct ttm_operation_ctx *ctx)
  468. {
  469. int64_t available;
  470. if (ctx->flags & TTM_OPT_FLAG_FORCE_ALLOC)
  471. return false;
  472. available = get_nr_swap_pages() + si_mem_available();
  473. available -= num_pages;
  474. if (available < glob->lower_mem_limit)
  475. return true;
  476. return false;
  477. }
  478. static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
  479. struct ttm_mem_zone *single_zone,
  480. uint64_t amount, bool reserve)
  481. {
  482. uint64_t limit;
  483. int ret = -ENOMEM;
  484. unsigned int i;
  485. struct ttm_mem_zone *zone;
  486. spin_lock(&glob->lock);
  487. for (i = 0; i < glob->num_zones; ++i) {
  488. zone = glob->zones[i];
  489. if (single_zone && zone != single_zone)
  490. continue;
  491. limit = (capable(CAP_SYS_ADMIN)) ?
  492. zone->emer_mem : zone->max_mem;
  493. if (zone->used_mem > limit)
  494. goto out_unlock;
  495. }
  496. if (reserve) {
  497. for (i = 0; i < glob->num_zones; ++i) {
  498. zone = glob->zones[i];
  499. if (single_zone && zone != single_zone)
  500. continue;
  501. zone->used_mem += amount;
  502. }
  503. }
  504. ret = 0;
  505. out_unlock:
  506. spin_unlock(&glob->lock);
  507. ttm_check_swapping(glob);
  508. return ret;
  509. }
  510. static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob,
  511. struct ttm_mem_zone *single_zone,
  512. uint64_t memory,
  513. struct ttm_operation_ctx *ctx)
  514. {
  515. int count = TTM_MEMORY_ALLOC_RETRIES;
  516. while (unlikely(ttm_mem_global_reserve(glob,
  517. single_zone,
  518. memory, true)
  519. != 0)) {
  520. if (ctx->no_wait_gpu)
  521. return -ENOMEM;
  522. if (unlikely(count-- == 0))
  523. return -ENOMEM;
  524. ttm_shrink(glob, false, memory + (memory >> 2) + 16, ctx);
  525. }
  526. return 0;
  527. }
  528. int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
  529. struct ttm_operation_ctx *ctx)
  530. {
  531. /**
  532. * Normal allocations of kernel memory are registered in
  533. * the kernel zone.
  534. */
  535. return ttm_mem_global_alloc_zone(glob, glob->zone_kernel, memory, ctx);
  536. }
  537. EXPORT_SYMBOL(ttm_mem_global_alloc);
  538. int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
  539. struct page *page, uint64_t size,
  540. struct ttm_operation_ctx *ctx)
  541. {
  542. struct ttm_mem_zone *zone = NULL;
  543. /**
  544. * Page allocations may be registed in a single zone
  545. * only if highmem or !dma32.
  546. */
  547. #ifdef CONFIG_HIGHMEM
  548. if (PageHighMem(page) && glob->zone_highmem != NULL)
  549. zone = glob->zone_highmem;
  550. #else
  551. if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
  552. zone = glob->zone_kernel;
  553. #endif
  554. return ttm_mem_global_alloc_zone(glob, zone, size, ctx);
  555. }
  556. void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page,
  557. uint64_t size)
  558. {
  559. struct ttm_mem_zone *zone = NULL;
  560. #ifdef CONFIG_HIGHMEM
  561. if (PageHighMem(page) && glob->zone_highmem != NULL)
  562. zone = glob->zone_highmem;
  563. #else
  564. if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
  565. zone = glob->zone_kernel;
  566. #endif
  567. ttm_mem_global_free_zone(glob, zone, size);
  568. }
  569. size_t ttm_round_pot(size_t size)
  570. {
  571. if ((size & (size - 1)) == 0)
  572. return size;
  573. else if (size > PAGE_SIZE)
  574. return PAGE_ALIGN(size);
  575. else {
  576. size_t tmp_size = 4;
  577. while (tmp_size < size)
  578. tmp_size <<= 1;
  579. return tmp_size;
  580. }
  581. return 0;
  582. }
  583. EXPORT_SYMBOL(ttm_round_pot);