mm.c 32 KB

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