mm.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /*
  2. * PS3 address space management.
  3. *
  4. * Copyright (C) 2006 Sony Computer Entertainment Inc.
  5. * Copyright 2006 Sony Corp.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/export.h>
  22. #include <linux/memblock.h>
  23. #include <linux/slab.h>
  24. #include <asm/cell-regs.h>
  25. #include <asm/firmware.h>
  26. #include <asm/prom.h>
  27. #include <asm/udbg.h>
  28. #include <asm/lv1call.h>
  29. #include <asm/setup.h>
  30. #include "platform.h"
  31. #if defined(DEBUG)
  32. #define DBG udbg_printf
  33. #else
  34. #define DBG pr_devel
  35. #endif
  36. enum {
  37. #if defined(CONFIG_PS3_DYNAMIC_DMA)
  38. USE_DYNAMIC_DMA = 1,
  39. #else
  40. USE_DYNAMIC_DMA = 0,
  41. #endif
  42. };
  43. enum {
  44. PAGE_SHIFT_4K = 12U,
  45. PAGE_SHIFT_64K = 16U,
  46. PAGE_SHIFT_16M = 24U,
  47. };
  48. static unsigned long make_page_sizes(unsigned long a, unsigned long b)
  49. {
  50. return (a << 56) | (b << 48);
  51. }
  52. enum {
  53. ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04,
  54. ALLOCATE_MEMORY_ADDR_ZERO = 0X08,
  55. };
  56. /* valid htab sizes are {18,19,20} = 256K, 512K, 1M */
  57. enum {
  58. HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */
  59. HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */
  60. };
  61. /*============================================================================*/
  62. /* virtual address space routines */
  63. /*============================================================================*/
  64. /**
  65. * struct mem_region - memory region structure
  66. * @base: base address
  67. * @size: size in bytes
  68. * @offset: difference between base and rm.size
  69. * @destroy: flag if region should be destroyed upon shutdown
  70. */
  71. struct mem_region {
  72. u64 base;
  73. u64 size;
  74. unsigned long offset;
  75. int destroy;
  76. };
  77. /**
  78. * struct map - address space state variables holder
  79. * @total: total memory available as reported by HV
  80. * @vas_id - HV virtual address space id
  81. * @htab_size: htab size in bytes
  82. *
  83. * The HV virtual address space (vas) allows for hotplug memory regions.
  84. * Memory regions can be created and destroyed in the vas at runtime.
  85. * @rm: real mode (bootmem) region
  86. * @r1: highmem region(s)
  87. *
  88. * ps3 addresses
  89. * virt_addr: a cpu 'translated' effective address
  90. * phys_addr: an address in what Linux thinks is the physical address space
  91. * lpar_addr: an address in the HV virtual address space
  92. * bus_addr: an io controller 'translated' address on a device bus
  93. */
  94. struct map {
  95. u64 total;
  96. u64 vas_id;
  97. u64 htab_size;
  98. struct mem_region rm;
  99. struct mem_region r1;
  100. };
  101. #define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__)
  102. static void __maybe_unused _debug_dump_map(const struct map *m,
  103. const char *func, int line)
  104. {
  105. DBG("%s:%d: map.total = %llxh\n", func, line, m->total);
  106. DBG("%s:%d: map.rm.size = %llxh\n", func, line, m->rm.size);
  107. DBG("%s:%d: map.vas_id = %llu\n", func, line, m->vas_id);
  108. DBG("%s:%d: map.htab_size = %llxh\n", func, line, m->htab_size);
  109. DBG("%s:%d: map.r1.base = %llxh\n", func, line, m->r1.base);
  110. DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset);
  111. DBG("%s:%d: map.r1.size = %llxh\n", func, line, m->r1.size);
  112. }
  113. static struct map map;
  114. /**
  115. * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address
  116. * @phys_addr: linux physical address
  117. */
  118. unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr)
  119. {
  120. BUG_ON(is_kernel_addr(phys_addr));
  121. return (phys_addr < map.rm.size || phys_addr >= map.total)
  122. ? phys_addr : phys_addr + map.r1.offset;
  123. }
  124. EXPORT_SYMBOL(ps3_mm_phys_to_lpar);
  125. /**
  126. * ps3_mm_vas_create - create the virtual address space
  127. */
  128. void __init ps3_mm_vas_create(unsigned long* htab_size)
  129. {
  130. int result;
  131. u64 start_address;
  132. u64 size;
  133. u64 access_right;
  134. u64 max_page_size;
  135. u64 flags;
  136. result = lv1_query_logical_partition_address_region_info(0,
  137. &start_address, &size, &access_right, &max_page_size,
  138. &flags);
  139. if (result) {
  140. DBG("%s:%d: lv1_query_logical_partition_address_region_info "
  141. "failed: %s\n", __func__, __LINE__,
  142. ps3_result(result));
  143. goto fail;
  144. }
  145. if (max_page_size < PAGE_SHIFT_16M) {
  146. DBG("%s:%d: bad max_page_size %llxh\n", __func__, __LINE__,
  147. max_page_size);
  148. goto fail;
  149. }
  150. BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE > HTAB_SIZE_MAX);
  151. BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE < HTAB_SIZE_MIN);
  152. result = lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE,
  153. 2, make_page_sizes(PAGE_SHIFT_16M, PAGE_SHIFT_64K),
  154. &map.vas_id, &map.htab_size);
  155. if (result) {
  156. DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n",
  157. __func__, __LINE__, ps3_result(result));
  158. goto fail;
  159. }
  160. result = lv1_select_virtual_address_space(map.vas_id);
  161. if (result) {
  162. DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n",
  163. __func__, __LINE__, ps3_result(result));
  164. goto fail;
  165. }
  166. *htab_size = map.htab_size;
  167. debug_dump_map(&map);
  168. return;
  169. fail:
  170. panic("ps3_mm_vas_create failed");
  171. }
  172. /**
  173. * ps3_mm_vas_destroy -
  174. */
  175. void ps3_mm_vas_destroy(void)
  176. {
  177. int result;
  178. DBG("%s:%d: map.vas_id = %llu\n", __func__, __LINE__, map.vas_id);
  179. if (map.vas_id) {
  180. result = lv1_select_virtual_address_space(0);
  181. BUG_ON(result);
  182. result = lv1_destruct_virtual_address_space(map.vas_id);
  183. BUG_ON(result);
  184. map.vas_id = 0;
  185. }
  186. }
  187. static int ps3_mm_get_repository_highmem(struct mem_region *r)
  188. {
  189. int result;
  190. /* Assume a single highmem region. */
  191. result = ps3_repository_read_highmem_info(0, &r->base, &r->size);
  192. if (result)
  193. goto zero_region;
  194. if (!r->base || !r->size) {
  195. result = -1;
  196. goto zero_region;
  197. }
  198. r->offset = r->base - map.rm.size;
  199. DBG("%s:%d: Found high region in repository: %llxh %llxh\n",
  200. __func__, __LINE__, r->base, r->size);
  201. return 0;
  202. zero_region:
  203. DBG("%s:%d: No high region in repository.\n", __func__, __LINE__);
  204. r->size = r->base = r->offset = 0;
  205. return result;
  206. }
  207. static int ps3_mm_set_repository_highmem(const struct mem_region *r)
  208. {
  209. /* Assume a single highmem region. */
  210. return r ? ps3_repository_write_highmem_info(0, r->base, r->size) :
  211. ps3_repository_write_highmem_info(0, 0, 0);
  212. }
  213. /**
  214. * ps3_mm_region_create - create a memory region in the vas
  215. * @r: pointer to a struct mem_region to accept initialized values
  216. * @size: requested region size
  217. *
  218. * This implementation creates the region with the vas large page size.
  219. * @size is rounded down to a multiple of the vas large page size.
  220. */
  221. static int ps3_mm_region_create(struct mem_region *r, unsigned long size)
  222. {
  223. int result;
  224. u64 muid;
  225. r->size = _ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M);
  226. DBG("%s:%d requested %lxh\n", __func__, __LINE__, size);
  227. DBG("%s:%d actual %llxh\n", __func__, __LINE__, r->size);
  228. DBG("%s:%d difference %llxh (%lluMB)\n", __func__, __LINE__,
  229. size - r->size, (size - r->size) / 1024 / 1024);
  230. if (r->size == 0) {
  231. DBG("%s:%d: size == 0\n", __func__, __LINE__);
  232. result = -1;
  233. goto zero_region;
  234. }
  235. result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0,
  236. ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid);
  237. if (result || r->base < map.rm.size) {
  238. DBG("%s:%d: lv1_allocate_memory failed: %s\n",
  239. __func__, __LINE__, ps3_result(result));
  240. goto zero_region;
  241. }
  242. r->destroy = 1;
  243. r->offset = r->base - map.rm.size;
  244. return result;
  245. zero_region:
  246. r->size = r->base = r->offset = 0;
  247. return result;
  248. }
  249. /**
  250. * ps3_mm_region_destroy - destroy a memory region
  251. * @r: pointer to struct mem_region
  252. */
  253. static void ps3_mm_region_destroy(struct mem_region *r)
  254. {
  255. int result;
  256. if (!r->destroy) {
  257. pr_info("%s:%d: Not destroying high region: %llxh %llxh\n",
  258. __func__, __LINE__, r->base, r->size);
  259. return;
  260. }
  261. DBG("%s:%d: r->base = %llxh\n", __func__, __LINE__, r->base);
  262. if (r->base) {
  263. result = lv1_release_memory(r->base);
  264. BUG_ON(result);
  265. r->size = r->base = r->offset = 0;
  266. map.total = map.rm.size;
  267. }
  268. ps3_mm_set_repository_highmem(NULL);
  269. }
  270. /*============================================================================*/
  271. /* dma routines */
  272. /*============================================================================*/
  273. /**
  274. * dma_sb_lpar_to_bus - Translate an lpar address to ioc mapped bus address.
  275. * @r: pointer to dma region structure
  276. * @lpar_addr: HV lpar address
  277. */
  278. static unsigned long dma_sb_lpar_to_bus(struct ps3_dma_region *r,
  279. unsigned long lpar_addr)
  280. {
  281. if (lpar_addr >= map.rm.size)
  282. lpar_addr -= map.r1.offset;
  283. BUG_ON(lpar_addr < r->offset);
  284. BUG_ON(lpar_addr >= r->offset + r->len);
  285. return r->bus_addr + lpar_addr - r->offset;
  286. }
  287. #define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__)
  288. static void __maybe_unused _dma_dump_region(const struct ps3_dma_region *r,
  289. const char *func, int line)
  290. {
  291. DBG("%s:%d: dev %llu:%llu\n", func, line, r->dev->bus_id,
  292. r->dev->dev_id);
  293. DBG("%s:%d: page_size %u\n", func, line, r->page_size);
  294. DBG("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr);
  295. DBG("%s:%d: len %lxh\n", func, line, r->len);
  296. DBG("%s:%d: offset %lxh\n", func, line, r->offset);
  297. }
  298. /**
  299. * dma_chunk - A chunk of dma pages mapped by the io controller.
  300. * @region - The dma region that owns this chunk.
  301. * @lpar_addr: Starting lpar address of the area to map.
  302. * @bus_addr: Starting ioc bus address of the area to map.
  303. * @len: Length in bytes of the area to map.
  304. * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the
  305. * list of all chuncks owned by the region.
  306. *
  307. * This implementation uses a very simple dma page manager
  308. * based on the dma_chunk structure. This scheme assumes
  309. * that all drivers use very well behaved dma ops.
  310. */
  311. struct dma_chunk {
  312. struct ps3_dma_region *region;
  313. unsigned long lpar_addr;
  314. unsigned long bus_addr;
  315. unsigned long len;
  316. struct list_head link;
  317. unsigned int usage_count;
  318. };
  319. #define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__)
  320. static void _dma_dump_chunk (const struct dma_chunk* c, const char* func,
  321. int line)
  322. {
  323. DBG("%s:%d: r.dev %llu:%llu\n", func, line,
  324. c->region->dev->bus_id, c->region->dev->dev_id);
  325. DBG("%s:%d: r.bus_addr %lxh\n", func, line, c->region->bus_addr);
  326. DBG("%s:%d: r.page_size %u\n", func, line, c->region->page_size);
  327. DBG("%s:%d: r.len %lxh\n", func, line, c->region->len);
  328. DBG("%s:%d: r.offset %lxh\n", func, line, c->region->offset);
  329. DBG("%s:%d: c.lpar_addr %lxh\n", func, line, c->lpar_addr);
  330. DBG("%s:%d: c.bus_addr %lxh\n", func, line, c->bus_addr);
  331. DBG("%s:%d: c.len %lxh\n", func, line, c->len);
  332. }
  333. static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r,
  334. unsigned long bus_addr, unsigned long len)
  335. {
  336. struct dma_chunk *c;
  337. unsigned long aligned_bus = _ALIGN_DOWN(bus_addr, 1 << r->page_size);
  338. unsigned long aligned_len = _ALIGN_UP(len+bus_addr-aligned_bus,
  339. 1 << r->page_size);
  340. list_for_each_entry(c, &r->chunk_list.head, link) {
  341. /* intersection */
  342. if (aligned_bus >= c->bus_addr &&
  343. aligned_bus + aligned_len <= c->bus_addr + c->len)
  344. return c;
  345. /* below */
  346. if (aligned_bus + aligned_len <= c->bus_addr)
  347. continue;
  348. /* above */
  349. if (aligned_bus >= c->bus_addr + c->len)
  350. continue;
  351. /* we don't handle the multi-chunk case for now */
  352. dma_dump_chunk(c);
  353. BUG();
  354. }
  355. return NULL;
  356. }
  357. static struct dma_chunk *dma_find_chunk_lpar(struct ps3_dma_region *r,
  358. unsigned long lpar_addr, unsigned long len)
  359. {
  360. struct dma_chunk *c;
  361. unsigned long aligned_lpar = _ALIGN_DOWN(lpar_addr, 1 << r->page_size);
  362. unsigned long aligned_len = _ALIGN_UP(len + lpar_addr - aligned_lpar,
  363. 1 << r->page_size);
  364. list_for_each_entry(c, &r->chunk_list.head, link) {
  365. /* intersection */
  366. if (c->lpar_addr <= aligned_lpar &&
  367. aligned_lpar < c->lpar_addr + c->len) {
  368. if (aligned_lpar + aligned_len <= c->lpar_addr + c->len)
  369. return c;
  370. else {
  371. dma_dump_chunk(c);
  372. BUG();
  373. }
  374. }
  375. /* below */
  376. if (aligned_lpar + aligned_len <= c->lpar_addr) {
  377. continue;
  378. }
  379. /* above */
  380. if (c->lpar_addr + c->len <= aligned_lpar) {
  381. continue;
  382. }
  383. }
  384. return NULL;
  385. }
  386. static int dma_sb_free_chunk(struct dma_chunk *c)
  387. {
  388. int result = 0;
  389. if (c->bus_addr) {
  390. result = lv1_unmap_device_dma_region(c->region->dev->bus_id,
  391. c->region->dev->dev_id, c->bus_addr, c->len);
  392. BUG_ON(result);
  393. }
  394. kfree(c);
  395. return result;
  396. }
  397. static int dma_ioc0_free_chunk(struct dma_chunk *c)
  398. {
  399. int result = 0;
  400. int iopage;
  401. unsigned long offset;
  402. struct ps3_dma_region *r = c->region;
  403. DBG("%s:start\n", __func__);
  404. for (iopage = 0; iopage < (c->len >> r->page_size); iopage++) {
  405. offset = (1 << r->page_size) * iopage;
  406. /* put INVALID entry */
  407. result = lv1_put_iopte(0,
  408. c->bus_addr + offset,
  409. c->lpar_addr + offset,
  410. r->ioid,
  411. 0);
  412. DBG("%s: bus=%#lx, lpar=%#lx, ioid=%d\n", __func__,
  413. c->bus_addr + offset,
  414. c->lpar_addr + offset,
  415. r->ioid);
  416. if (result) {
  417. DBG("%s:%d: lv1_put_iopte failed: %s\n", __func__,
  418. __LINE__, ps3_result(result));
  419. }
  420. }
  421. kfree(c);
  422. DBG("%s:end\n", __func__);
  423. return result;
  424. }
  425. /**
  426. * dma_sb_map_pages - Maps dma pages into the io controller bus address space.
  427. * @r: Pointer to a struct ps3_dma_region.
  428. * @phys_addr: Starting physical address of the area to map.
  429. * @len: Length in bytes of the area to map.
  430. * c_out: A pointer to receive an allocated struct dma_chunk for this area.
  431. *
  432. * This is the lowest level dma mapping routine, and is the one that will
  433. * make the HV call to add the pages into the io controller address space.
  434. */
  435. static int dma_sb_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
  436. unsigned long len, struct dma_chunk **c_out, u64 iopte_flag)
  437. {
  438. int result;
  439. struct dma_chunk *c;
  440. c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);
  441. if (!c) {
  442. result = -ENOMEM;
  443. goto fail_alloc;
  444. }
  445. c->region = r;
  446. c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
  447. c->bus_addr = dma_sb_lpar_to_bus(r, c->lpar_addr);
  448. c->len = len;
  449. BUG_ON(iopte_flag != 0xf800000000000000UL);
  450. result = lv1_map_device_dma_region(c->region->dev->bus_id,
  451. c->region->dev->dev_id, c->lpar_addr,
  452. c->bus_addr, c->len, iopte_flag);
  453. if (result) {
  454. DBG("%s:%d: lv1_map_device_dma_region failed: %s\n",
  455. __func__, __LINE__, ps3_result(result));
  456. goto fail_map;
  457. }
  458. list_add(&c->link, &r->chunk_list.head);
  459. *c_out = c;
  460. return 0;
  461. fail_map:
  462. kfree(c);
  463. fail_alloc:
  464. *c_out = NULL;
  465. DBG(" <- %s:%d\n", __func__, __LINE__);
  466. return result;
  467. }
  468. static int dma_ioc0_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
  469. unsigned long len, struct dma_chunk **c_out,
  470. u64 iopte_flag)
  471. {
  472. int result;
  473. struct dma_chunk *c, *last;
  474. int iopage, pages;
  475. unsigned long offset;
  476. DBG(KERN_ERR "%s: phy=%#lx, lpar%#lx, len=%#lx\n", __func__,
  477. phys_addr, ps3_mm_phys_to_lpar(phys_addr), len);
  478. c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);
  479. if (!c) {
  480. result = -ENOMEM;
  481. goto fail_alloc;
  482. }
  483. c->region = r;
  484. c->len = len;
  485. c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
  486. /* allocate IO address */
  487. if (list_empty(&r->chunk_list.head)) {
  488. /* first one */
  489. c->bus_addr = r->bus_addr;
  490. } else {
  491. /* derive from last bus addr*/
  492. last = list_entry(r->chunk_list.head.next,
  493. struct dma_chunk, link);
  494. c->bus_addr = last->bus_addr + last->len;
  495. DBG("%s: last bus=%#lx, len=%#lx\n", __func__,
  496. last->bus_addr, last->len);
  497. }
  498. /* FIXME: check whether length exceeds region size */
  499. /* build ioptes for the area */
  500. pages = len >> r->page_size;
  501. DBG("%s: pgsize=%#x len=%#lx pages=%#x iopteflag=%#llx\n", __func__,
  502. r->page_size, r->len, pages, iopte_flag);
  503. for (iopage = 0; iopage < pages; iopage++) {
  504. offset = (1 << r->page_size) * iopage;
  505. result = lv1_put_iopte(0,
  506. c->bus_addr + offset,
  507. c->lpar_addr + offset,
  508. r->ioid,
  509. iopte_flag);
  510. if (result) {
  511. pr_warning("%s:%d: lv1_put_iopte failed: %s\n",
  512. __func__, __LINE__, ps3_result(result));
  513. goto fail_map;
  514. }
  515. DBG("%s: pg=%d bus=%#lx, lpar=%#lx, ioid=%#x\n", __func__,
  516. iopage, c->bus_addr + offset, c->lpar_addr + offset,
  517. r->ioid);
  518. }
  519. /* be sure that last allocated one is inserted at head */
  520. list_add(&c->link, &r->chunk_list.head);
  521. *c_out = c;
  522. DBG("%s: end\n", __func__);
  523. return 0;
  524. fail_map:
  525. for (iopage--; 0 <= iopage; iopage--) {
  526. lv1_put_iopte(0,
  527. c->bus_addr + offset,
  528. c->lpar_addr + offset,
  529. r->ioid,
  530. 0);
  531. }
  532. kfree(c);
  533. fail_alloc:
  534. *c_out = NULL;
  535. return result;
  536. }
  537. /**
  538. * dma_sb_region_create - Create a device dma region.
  539. * @r: Pointer to a struct ps3_dma_region.
  540. *
  541. * This is the lowest level dma region create routine, and is the one that
  542. * will make the HV call to create the region.
  543. */
  544. static int dma_sb_region_create(struct ps3_dma_region *r)
  545. {
  546. int result;
  547. u64 bus_addr;
  548. DBG(" -> %s:%d:\n", __func__, __LINE__);
  549. BUG_ON(!r);
  550. if (!r->dev->bus_id) {
  551. pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
  552. r->dev->bus_id, r->dev->dev_id);
  553. return 0;
  554. }
  555. DBG("%s:%u: len = 0x%lx, page_size = %u, offset = 0x%lx\n", __func__,
  556. __LINE__, r->len, r->page_size, r->offset);
  557. BUG_ON(!r->len);
  558. BUG_ON(!r->page_size);
  559. BUG_ON(!r->region_ops);
  560. INIT_LIST_HEAD(&r->chunk_list.head);
  561. spin_lock_init(&r->chunk_list.lock);
  562. result = lv1_allocate_device_dma_region(r->dev->bus_id, r->dev->dev_id,
  563. roundup_pow_of_two(r->len), r->page_size, r->region_type,
  564. &bus_addr);
  565. r->bus_addr = bus_addr;
  566. if (result) {
  567. DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n",
  568. __func__, __LINE__, ps3_result(result));
  569. r->len = r->bus_addr = 0;
  570. }
  571. return result;
  572. }
  573. static int dma_ioc0_region_create(struct ps3_dma_region *r)
  574. {
  575. int result;
  576. u64 bus_addr;
  577. INIT_LIST_HEAD(&r->chunk_list.head);
  578. spin_lock_init(&r->chunk_list.lock);
  579. result = lv1_allocate_io_segment(0,
  580. r->len,
  581. r->page_size,
  582. &bus_addr);
  583. r->bus_addr = bus_addr;
  584. if (result) {
  585. DBG("%s:%d: lv1_allocate_io_segment failed: %s\n",
  586. __func__, __LINE__, ps3_result(result));
  587. r->len = r->bus_addr = 0;
  588. }
  589. DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__,
  590. r->len, r->page_size, r->bus_addr);
  591. return result;
  592. }
  593. /**
  594. * dma_region_free - Free a device dma region.
  595. * @r: Pointer to a struct ps3_dma_region.
  596. *
  597. * This is the lowest level dma region free routine, and is the one that
  598. * will make the HV call to free the region.
  599. */
  600. static int dma_sb_region_free(struct ps3_dma_region *r)
  601. {
  602. int result;
  603. struct dma_chunk *c;
  604. struct dma_chunk *tmp;
  605. BUG_ON(!r);
  606. if (!r->dev->bus_id) {
  607. pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
  608. r->dev->bus_id, r->dev->dev_id);
  609. return 0;
  610. }
  611. list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) {
  612. list_del(&c->link);
  613. dma_sb_free_chunk(c);
  614. }
  615. result = lv1_free_device_dma_region(r->dev->bus_id, r->dev->dev_id,
  616. r->bus_addr);
  617. if (result)
  618. DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
  619. __func__, __LINE__, ps3_result(result));
  620. r->bus_addr = 0;
  621. return result;
  622. }
  623. static int dma_ioc0_region_free(struct ps3_dma_region *r)
  624. {
  625. int result;
  626. struct dma_chunk *c, *n;
  627. DBG("%s: start\n", __func__);
  628. list_for_each_entry_safe(c, n, &r->chunk_list.head, link) {
  629. list_del(&c->link);
  630. dma_ioc0_free_chunk(c);
  631. }
  632. result = lv1_release_io_segment(0, r->bus_addr);
  633. if (result)
  634. DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
  635. __func__, __LINE__, ps3_result(result));
  636. r->bus_addr = 0;
  637. DBG("%s: end\n", __func__);
  638. return result;
  639. }
  640. /**
  641. * dma_sb_map_area - Map an area of memory into a device dma region.
  642. * @r: Pointer to a struct ps3_dma_region.
  643. * @virt_addr: Starting virtual address of the area to map.
  644. * @len: Length in bytes of the area to map.
  645. * @bus_addr: A pointer to return the starting ioc bus address of the area to
  646. * map.
  647. *
  648. * This is the common dma mapping routine.
  649. */
  650. static int dma_sb_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
  651. unsigned long len, dma_addr_t *bus_addr,
  652. u64 iopte_flag)
  653. {
  654. int result;
  655. unsigned long flags;
  656. struct dma_chunk *c;
  657. unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
  658. : virt_addr;
  659. unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
  660. unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
  661. 1 << r->page_size);
  662. *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
  663. if (!USE_DYNAMIC_DMA) {
  664. unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
  665. DBG(" -> %s:%d\n", __func__, __LINE__);
  666. DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__,
  667. virt_addr);
  668. DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__,
  669. phys_addr);
  670. DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__,
  671. lpar_addr);
  672. DBG("%s:%d len %lxh\n", __func__, __LINE__, len);
  673. DBG("%s:%d bus_addr %llxh (%lxh)\n", __func__, __LINE__,
  674. *bus_addr, len);
  675. }
  676. spin_lock_irqsave(&r->chunk_list.lock, flags);
  677. c = dma_find_chunk(r, *bus_addr, len);
  678. if (c) {
  679. DBG("%s:%d: reusing mapped chunk", __func__, __LINE__);
  680. dma_dump_chunk(c);
  681. c->usage_count++;
  682. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  683. return 0;
  684. }
  685. result = dma_sb_map_pages(r, aligned_phys, aligned_len, &c, iopte_flag);
  686. if (result) {
  687. *bus_addr = 0;
  688. DBG("%s:%d: dma_sb_map_pages failed (%d)\n",
  689. __func__, __LINE__, result);
  690. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  691. return result;
  692. }
  693. c->usage_count = 1;
  694. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  695. return result;
  696. }
  697. static int dma_ioc0_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
  698. unsigned long len, dma_addr_t *bus_addr,
  699. u64 iopte_flag)
  700. {
  701. int result;
  702. unsigned long flags;
  703. struct dma_chunk *c;
  704. unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
  705. : virt_addr;
  706. unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
  707. unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
  708. 1 << r->page_size);
  709. DBG(KERN_ERR "%s: vaddr=%#lx, len=%#lx\n", __func__,
  710. virt_addr, len);
  711. DBG(KERN_ERR "%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__,
  712. phys_addr, aligned_phys, aligned_len);
  713. spin_lock_irqsave(&r->chunk_list.lock, flags);
  714. c = dma_find_chunk_lpar(r, ps3_mm_phys_to_lpar(phys_addr), len);
  715. if (c) {
  716. /* FIXME */
  717. BUG();
  718. *bus_addr = c->bus_addr + phys_addr - aligned_phys;
  719. c->usage_count++;
  720. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  721. return 0;
  722. }
  723. result = dma_ioc0_map_pages(r, aligned_phys, aligned_len, &c,
  724. iopte_flag);
  725. if (result) {
  726. *bus_addr = 0;
  727. DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n",
  728. __func__, __LINE__, result);
  729. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  730. return result;
  731. }
  732. *bus_addr = c->bus_addr + phys_addr - aligned_phys;
  733. DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#llx\n", __func__,
  734. virt_addr, phys_addr, aligned_phys, *bus_addr);
  735. c->usage_count = 1;
  736. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  737. return result;
  738. }
  739. /**
  740. * dma_sb_unmap_area - Unmap an area of memory from a device dma region.
  741. * @r: Pointer to a struct ps3_dma_region.
  742. * @bus_addr: The starting ioc bus address of the area to unmap.
  743. * @len: Length in bytes of the area to unmap.
  744. *
  745. * This is the common dma unmap routine.
  746. */
  747. static int dma_sb_unmap_area(struct ps3_dma_region *r, dma_addr_t bus_addr,
  748. unsigned long len)
  749. {
  750. unsigned long flags;
  751. struct dma_chunk *c;
  752. spin_lock_irqsave(&r->chunk_list.lock, flags);
  753. c = dma_find_chunk(r, bus_addr, len);
  754. if (!c) {
  755. unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
  756. 1 << r->page_size);
  757. unsigned long aligned_len = _ALIGN_UP(len + bus_addr
  758. - aligned_bus, 1 << r->page_size);
  759. DBG("%s:%d: not found: bus_addr %llxh\n",
  760. __func__, __LINE__, bus_addr);
  761. DBG("%s:%d: not found: len %lxh\n",
  762. __func__, __LINE__, len);
  763. DBG("%s:%d: not found: aligned_bus %lxh\n",
  764. __func__, __LINE__, aligned_bus);
  765. DBG("%s:%d: not found: aligned_len %lxh\n",
  766. __func__, __LINE__, aligned_len);
  767. BUG();
  768. }
  769. c->usage_count--;
  770. if (!c->usage_count) {
  771. list_del(&c->link);
  772. dma_sb_free_chunk(c);
  773. }
  774. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  775. return 0;
  776. }
  777. static int dma_ioc0_unmap_area(struct ps3_dma_region *r,
  778. dma_addr_t bus_addr, unsigned long len)
  779. {
  780. unsigned long flags;
  781. struct dma_chunk *c;
  782. DBG("%s: start a=%#llx l=%#lx\n", __func__, bus_addr, len);
  783. spin_lock_irqsave(&r->chunk_list.lock, flags);
  784. c = dma_find_chunk(r, bus_addr, len);
  785. if (!c) {
  786. unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
  787. 1 << r->page_size);
  788. unsigned long aligned_len = _ALIGN_UP(len + bus_addr
  789. - aligned_bus,
  790. 1 << r->page_size);
  791. DBG("%s:%d: not found: bus_addr %llxh\n",
  792. __func__, __LINE__, bus_addr);
  793. DBG("%s:%d: not found: len %lxh\n",
  794. __func__, __LINE__, len);
  795. DBG("%s:%d: not found: aligned_bus %lxh\n",
  796. __func__, __LINE__, aligned_bus);
  797. DBG("%s:%d: not found: aligned_len %lxh\n",
  798. __func__, __LINE__, aligned_len);
  799. BUG();
  800. }
  801. c->usage_count--;
  802. if (!c->usage_count) {
  803. list_del(&c->link);
  804. dma_ioc0_free_chunk(c);
  805. }
  806. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  807. DBG("%s: end\n", __func__);
  808. return 0;
  809. }
  810. /**
  811. * dma_sb_region_create_linear - Setup a linear dma mapping for a device.
  812. * @r: Pointer to a struct ps3_dma_region.
  813. *
  814. * This routine creates an HV dma region for the device and maps all available
  815. * ram into the io controller bus address space.
  816. */
  817. static int dma_sb_region_create_linear(struct ps3_dma_region *r)
  818. {
  819. int result;
  820. unsigned long virt_addr, len;
  821. dma_addr_t tmp;
  822. if (r->len > 16*1024*1024) { /* FIXME: need proper fix */
  823. /* force 16M dma pages for linear mapping */
  824. if (r->page_size != PS3_DMA_16M) {
  825. pr_info("%s:%d: forcing 16M pages for linear map\n",
  826. __func__, __LINE__);
  827. r->page_size = PS3_DMA_16M;
  828. r->len = _ALIGN_UP(r->len, 1 << r->page_size);
  829. }
  830. }
  831. result = dma_sb_region_create(r);
  832. BUG_ON(result);
  833. if (r->offset < map.rm.size) {
  834. /* Map (part of) 1st RAM chunk */
  835. virt_addr = map.rm.base + r->offset;
  836. len = map.rm.size - r->offset;
  837. if (len > r->len)
  838. len = r->len;
  839. result = dma_sb_map_area(r, virt_addr, len, &tmp,
  840. CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
  841. CBE_IOPTE_M);
  842. BUG_ON(result);
  843. }
  844. if (r->offset + r->len > map.rm.size) {
  845. /* Map (part of) 2nd RAM chunk */
  846. virt_addr = map.rm.size;
  847. len = r->len;
  848. if (r->offset >= map.rm.size)
  849. virt_addr += r->offset - map.rm.size;
  850. else
  851. len -= map.rm.size - r->offset;
  852. result = dma_sb_map_area(r, virt_addr, len, &tmp,
  853. CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
  854. CBE_IOPTE_M);
  855. BUG_ON(result);
  856. }
  857. return result;
  858. }
  859. /**
  860. * dma_sb_region_free_linear - Free a linear dma mapping for a device.
  861. * @r: Pointer to a struct ps3_dma_region.
  862. *
  863. * This routine will unmap all mapped areas and free the HV dma region.
  864. */
  865. static int dma_sb_region_free_linear(struct ps3_dma_region *r)
  866. {
  867. int result;
  868. dma_addr_t bus_addr;
  869. unsigned long len, lpar_addr;
  870. if (r->offset < map.rm.size) {
  871. /* Unmap (part of) 1st RAM chunk */
  872. lpar_addr = map.rm.base + r->offset;
  873. len = map.rm.size - r->offset;
  874. if (len > r->len)
  875. len = r->len;
  876. bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
  877. result = dma_sb_unmap_area(r, bus_addr, len);
  878. BUG_ON(result);
  879. }
  880. if (r->offset + r->len > map.rm.size) {
  881. /* Unmap (part of) 2nd RAM chunk */
  882. lpar_addr = map.r1.base;
  883. len = r->len;
  884. if (r->offset >= map.rm.size)
  885. lpar_addr += r->offset - map.rm.size;
  886. else
  887. len -= map.rm.size - r->offset;
  888. bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
  889. result = dma_sb_unmap_area(r, bus_addr, len);
  890. BUG_ON(result);
  891. }
  892. result = dma_sb_region_free(r);
  893. BUG_ON(result);
  894. return result;
  895. }
  896. /**
  897. * dma_sb_map_area_linear - Map an area of memory into a device dma region.
  898. * @r: Pointer to a struct ps3_dma_region.
  899. * @virt_addr: Starting virtual address of the area to map.
  900. * @len: Length in bytes of the area to map.
  901. * @bus_addr: A pointer to return the starting ioc bus address of the area to
  902. * map.
  903. *
  904. * This routine just returns the corresponding bus address. Actual mapping
  905. * occurs in dma_region_create_linear().
  906. */
  907. static int dma_sb_map_area_linear(struct ps3_dma_region *r,
  908. unsigned long virt_addr, unsigned long len, dma_addr_t *bus_addr,
  909. u64 iopte_flag)
  910. {
  911. unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
  912. : virt_addr;
  913. *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
  914. return 0;
  915. }
  916. /**
  917. * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
  918. * @r: Pointer to a struct ps3_dma_region.
  919. * @bus_addr: The starting ioc bus address of the area to unmap.
  920. * @len: Length in bytes of the area to unmap.
  921. *
  922. * This routine does nothing. Unmapping occurs in dma_sb_region_free_linear().
  923. */
  924. static int dma_sb_unmap_area_linear(struct ps3_dma_region *r,
  925. dma_addr_t bus_addr, unsigned long len)
  926. {
  927. return 0;
  928. };
  929. static const struct ps3_dma_region_ops ps3_dma_sb_region_ops = {
  930. .create = dma_sb_region_create,
  931. .free = dma_sb_region_free,
  932. .map = dma_sb_map_area,
  933. .unmap = dma_sb_unmap_area
  934. };
  935. static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = {
  936. .create = dma_sb_region_create_linear,
  937. .free = dma_sb_region_free_linear,
  938. .map = dma_sb_map_area_linear,
  939. .unmap = dma_sb_unmap_area_linear
  940. };
  941. static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = {
  942. .create = dma_ioc0_region_create,
  943. .free = dma_ioc0_region_free,
  944. .map = dma_ioc0_map_area,
  945. .unmap = dma_ioc0_unmap_area
  946. };
  947. int ps3_dma_region_init(struct ps3_system_bus_device *dev,
  948. struct ps3_dma_region *r, enum ps3_dma_page_size page_size,
  949. enum ps3_dma_region_type region_type, void *addr, unsigned long len)
  950. {
  951. unsigned long lpar_addr;
  952. lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0;
  953. r->dev = dev;
  954. r->page_size = page_size;
  955. r->region_type = region_type;
  956. r->offset = lpar_addr;
  957. if (r->offset >= map.rm.size)
  958. r->offset -= map.r1.offset;
  959. r->len = len ? len : _ALIGN_UP(map.total, 1 << r->page_size);
  960. switch (dev->dev_type) {
  961. case PS3_DEVICE_TYPE_SB:
  962. r->region_ops = (USE_DYNAMIC_DMA)
  963. ? &ps3_dma_sb_region_ops
  964. : &ps3_dma_sb_region_linear_ops;
  965. break;
  966. case PS3_DEVICE_TYPE_IOC0:
  967. r->region_ops = &ps3_dma_ioc0_region_ops;
  968. break;
  969. default:
  970. BUG();
  971. return -EINVAL;
  972. }
  973. return 0;
  974. }
  975. EXPORT_SYMBOL(ps3_dma_region_init);
  976. int ps3_dma_region_create(struct ps3_dma_region *r)
  977. {
  978. BUG_ON(!r);
  979. BUG_ON(!r->region_ops);
  980. BUG_ON(!r->region_ops->create);
  981. return r->region_ops->create(r);
  982. }
  983. EXPORT_SYMBOL(ps3_dma_region_create);
  984. int ps3_dma_region_free(struct ps3_dma_region *r)
  985. {
  986. BUG_ON(!r);
  987. BUG_ON(!r->region_ops);
  988. BUG_ON(!r->region_ops->free);
  989. return r->region_ops->free(r);
  990. }
  991. EXPORT_SYMBOL(ps3_dma_region_free);
  992. int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr,
  993. unsigned long len, dma_addr_t *bus_addr,
  994. u64 iopte_flag)
  995. {
  996. return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag);
  997. }
  998. int ps3_dma_unmap(struct ps3_dma_region *r, dma_addr_t bus_addr,
  999. unsigned long len)
  1000. {
  1001. return r->region_ops->unmap(r, bus_addr, len);
  1002. }
  1003. /*============================================================================*/
  1004. /* system startup routines */
  1005. /*============================================================================*/
  1006. /**
  1007. * ps3_mm_init - initialize the address space state variables
  1008. */
  1009. void __init ps3_mm_init(void)
  1010. {
  1011. int result;
  1012. DBG(" -> %s:%d\n", __func__, __LINE__);
  1013. result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size,
  1014. &map.total);
  1015. if (result)
  1016. panic("ps3_repository_read_mm_info() failed");
  1017. map.rm.offset = map.rm.base;
  1018. map.vas_id = map.htab_size = 0;
  1019. /* this implementation assumes map.rm.base is zero */
  1020. BUG_ON(map.rm.base);
  1021. BUG_ON(!map.rm.size);
  1022. /* Check if we got the highmem region from an earlier boot step */
  1023. if (ps3_mm_get_repository_highmem(&map.r1)) {
  1024. result = ps3_mm_region_create(&map.r1, map.total - map.rm.size);
  1025. if (!result)
  1026. ps3_mm_set_repository_highmem(&map.r1);
  1027. }
  1028. /* correct map.total for the real total amount of memory we use */
  1029. map.total = map.rm.size + map.r1.size;
  1030. if (!map.r1.size) {
  1031. DBG("%s:%d: No highmem region found\n", __func__, __LINE__);
  1032. } else {
  1033. DBG("%s:%d: Adding highmem region: %llxh %llxh\n",
  1034. __func__, __LINE__, map.rm.size,
  1035. map.total - map.rm.size);
  1036. memblock_add(map.rm.size, map.total - map.rm.size);
  1037. }
  1038. DBG(" <- %s:%d\n", __func__, __LINE__);
  1039. }
  1040. /**
  1041. * ps3_mm_shutdown - final cleanup of address space
  1042. */
  1043. void ps3_mm_shutdown(void)
  1044. {
  1045. ps3_mm_region_destroy(&map.r1);
  1046. }