mm.c 32 KB

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