mm.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  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/iommu.h>
  26. #include <asm/prom.h>
  27. #include <asm/udbg.h>
  28. #include <asm/lv1call.h>
  29. #include "platform.h"
  30. #if defined(DEBUG)
  31. #define DBG udbg_printf
  32. #else
  33. #define DBG pr_debug
  34. #endif
  35. enum {
  36. #if defined(CONFIG_PS3_DYNAMIC_DMA)
  37. USE_DYNAMIC_DMA = 1,
  38. #else
  39. USE_DYNAMIC_DMA = 0,
  40. #endif
  41. };
  42. enum {
  43. PAGE_SHIFT_4K = 12U,
  44. PAGE_SHIFT_64K = 16U,
  45. PAGE_SHIFT_16M = 24U,
  46. };
  47. static unsigned long make_page_sizes(unsigned long a, unsigned long b)
  48. {
  49. return (a << 56) | (b << 48);
  50. }
  51. enum {
  52. ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04,
  53. ALLOCATE_MEMORY_ADDR_ZERO = 0X08,
  54. };
  55. /* valid htab sizes are {18,19,20} = 256K, 512K, 1M */
  56. enum {
  57. HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */
  58. HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */
  59. };
  60. /*============================================================================*/
  61. /* virtual address space routines */
  62. /*============================================================================*/
  63. /**
  64. * struct mem_region - memory region structure
  65. * @base: base address
  66. * @size: size in bytes
  67. * @offset: difference between base and rm.size
  68. */
  69. struct mem_region {
  70. u64 base;
  71. u64 size;
  72. unsigned long offset;
  73. };
  74. /**
  75. * struct map - address space state variables holder
  76. * @total: total memory available as reported by HV
  77. * @vas_id - HV virtual address space id
  78. * @htab_size: htab size in bytes
  79. *
  80. * The HV virtual address space (vas) allows for hotplug memory regions.
  81. * Memory regions can be created and destroyed in the vas at runtime.
  82. * @rm: real mode (bootmem) region
  83. * @r1: hotplug memory region(s)
  84. *
  85. * ps3 addresses
  86. * virt_addr: a cpu 'translated' effective address
  87. * phys_addr: an address in what Linux thinks is the physical address space
  88. * lpar_addr: an address in the HV virtual address space
  89. * bus_addr: an io controller 'translated' address on a device bus
  90. */
  91. struct map {
  92. u64 total;
  93. u64 vas_id;
  94. u64 htab_size;
  95. struct mem_region rm;
  96. struct mem_region r1;
  97. };
  98. #define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__)
  99. static void __maybe_unused _debug_dump_map(const struct map *m,
  100. const char *func, int line)
  101. {
  102. DBG("%s:%d: map.total = %llxh\n", func, line, m->total);
  103. DBG("%s:%d: map.rm.size = %llxh\n", func, line, m->rm.size);
  104. DBG("%s:%d: map.vas_id = %llu\n", func, line, m->vas_id);
  105. DBG("%s:%d: map.htab_size = %llxh\n", func, line, m->htab_size);
  106. DBG("%s:%d: map.r1.base = %llxh\n", func, line, m->r1.base);
  107. DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset);
  108. DBG("%s:%d: map.r1.size = %llxh\n", func, line, m->r1.size);
  109. }
  110. static struct map map;
  111. /**
  112. * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address
  113. * @phys_addr: linux physical address
  114. */
  115. unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr)
  116. {
  117. BUG_ON(is_kernel_addr(phys_addr));
  118. return (phys_addr < map.rm.size || phys_addr >= map.total)
  119. ? phys_addr : phys_addr + map.r1.offset;
  120. }
  121. EXPORT_SYMBOL(ps3_mm_phys_to_lpar);
  122. /**
  123. * ps3_mm_vas_create - create the virtual address space
  124. */
  125. void __init ps3_mm_vas_create(unsigned long* htab_size)
  126. {
  127. int result;
  128. u64 start_address;
  129. u64 size;
  130. u64 access_right;
  131. u64 max_page_size;
  132. u64 flags;
  133. result = lv1_query_logical_partition_address_region_info(0,
  134. &start_address, &size, &access_right, &max_page_size,
  135. &flags);
  136. if (result) {
  137. DBG("%s:%d: lv1_query_logical_partition_address_region_info "
  138. "failed: %s\n", __func__, __LINE__,
  139. ps3_result(result));
  140. goto fail;
  141. }
  142. if (max_page_size < PAGE_SHIFT_16M) {
  143. DBG("%s:%d: bad max_page_size %llxh\n", __func__, __LINE__,
  144. max_page_size);
  145. goto fail;
  146. }
  147. BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE > HTAB_SIZE_MAX);
  148. BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE < HTAB_SIZE_MIN);
  149. result = lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE,
  150. 2, make_page_sizes(PAGE_SHIFT_16M, PAGE_SHIFT_64K),
  151. &map.vas_id, &map.htab_size);
  152. if (result) {
  153. DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n",
  154. __func__, __LINE__, ps3_result(result));
  155. goto fail;
  156. }
  157. result = lv1_select_virtual_address_space(map.vas_id);
  158. if (result) {
  159. DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n",
  160. __func__, __LINE__, ps3_result(result));
  161. goto fail;
  162. }
  163. *htab_size = map.htab_size;
  164. debug_dump_map(&map);
  165. return;
  166. fail:
  167. panic("ps3_mm_vas_create failed");
  168. }
  169. /**
  170. * ps3_mm_vas_destroy -
  171. */
  172. void ps3_mm_vas_destroy(void)
  173. {
  174. int result;
  175. DBG("%s:%d: map.vas_id = %llu\n", __func__, __LINE__, map.vas_id);
  176. if (map.vas_id) {
  177. result = lv1_select_virtual_address_space(0);
  178. BUG_ON(result);
  179. result = lv1_destruct_virtual_address_space(map.vas_id);
  180. BUG_ON(result);
  181. map.vas_id = 0;
  182. }
  183. }
  184. /*============================================================================*/
  185. /* memory hotplug routines */
  186. /*============================================================================*/
  187. /**
  188. * ps3_mm_region_create - create a memory region in the vas
  189. * @r: pointer to a struct mem_region to accept initialized values
  190. * @size: requested region size
  191. *
  192. * This implementation creates the region with the vas large page size.
  193. * @size is rounded down to a multiple of the vas large page size.
  194. */
  195. static int ps3_mm_region_create(struct mem_region *r, unsigned long size)
  196. {
  197. int result;
  198. u64 muid;
  199. r->size = _ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M);
  200. DBG("%s:%d requested %lxh\n", __func__, __LINE__, size);
  201. DBG("%s:%d actual %llxh\n", __func__, __LINE__, r->size);
  202. DBG("%s:%d difference %llxh (%lluMB)\n", __func__, __LINE__,
  203. size - r->size, (size - r->size) / 1024 / 1024);
  204. if (r->size == 0) {
  205. DBG("%s:%d: size == 0\n", __func__, __LINE__);
  206. result = -1;
  207. goto zero_region;
  208. }
  209. result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0,
  210. ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid);
  211. if (result || r->base < map.rm.size) {
  212. DBG("%s:%d: lv1_allocate_memory failed: %s\n",
  213. __func__, __LINE__, ps3_result(result));
  214. goto zero_region;
  215. }
  216. r->offset = r->base - map.rm.size;
  217. return result;
  218. zero_region:
  219. r->size = r->base = r->offset = 0;
  220. return result;
  221. }
  222. /**
  223. * ps3_mm_region_destroy - destroy a memory region
  224. * @r: pointer to struct mem_region
  225. */
  226. static void ps3_mm_region_destroy(struct mem_region *r)
  227. {
  228. int result;
  229. DBG("%s:%d: r->base = %llxh\n", __func__, __LINE__, r->base);
  230. if (r->base) {
  231. result = lv1_release_memory(r->base);
  232. BUG_ON(result);
  233. r->size = r->base = r->offset = 0;
  234. map.total = map.rm.size;
  235. }
  236. }
  237. /**
  238. * ps3_mm_add_memory - hot add memory
  239. */
  240. static int __init ps3_mm_add_memory(void)
  241. {
  242. int result;
  243. unsigned long start_addr;
  244. unsigned long start_pfn;
  245. unsigned long nr_pages;
  246. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  247. return -ENODEV;
  248. BUG_ON(!mem_init_done);
  249. start_addr = map.rm.size;
  250. start_pfn = start_addr >> PAGE_SHIFT;
  251. nr_pages = (map.r1.size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  252. DBG("%s:%d: start_addr %lxh, start_pfn %lxh, nr_pages %lxh\n",
  253. __func__, __LINE__, start_addr, start_pfn, nr_pages);
  254. result = add_memory(0, start_addr, map.r1.size);
  255. if (result) {
  256. pr_err("%s:%d: add_memory failed: (%d)\n",
  257. __func__, __LINE__, result);
  258. return result;
  259. }
  260. lmb_add(start_addr, map.r1.size);
  261. lmb_analyze();
  262. result = online_pages(start_pfn, nr_pages);
  263. if (result)
  264. pr_err("%s:%d: online_pages failed: (%d)\n",
  265. __func__, __LINE__, result);
  266. return result;
  267. }
  268. device_initcall(ps3_mm_add_memory);
  269. /*============================================================================*/
  270. /* dma routines */
  271. /*============================================================================*/
  272. /**
  273. * dma_sb_lpar_to_bus - Translate an lpar address to ioc mapped bus address.
  274. * @r: pointer to dma region structure
  275. * @lpar_addr: HV lpar address
  276. */
  277. static unsigned long dma_sb_lpar_to_bus(struct ps3_dma_region *r,
  278. unsigned long lpar_addr)
  279. {
  280. if (lpar_addr >= map.rm.size)
  281. lpar_addr -= map.r1.offset;
  282. BUG_ON(lpar_addr < r->offset);
  283. BUG_ON(lpar_addr >= r->offset + r->len);
  284. return r->bus_addr + lpar_addr - r->offset;
  285. }
  286. #define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__)
  287. static void __maybe_unused _dma_dump_region(const struct ps3_dma_region *r,
  288. const char *func, int line)
  289. {
  290. DBG("%s:%d: dev %llu:%llu\n", func, line, r->dev->bus_id,
  291. r->dev->dev_id);
  292. DBG("%s:%d: page_size %u\n", func, line, r->page_size);
  293. DBG("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr);
  294. DBG("%s:%d: len %lxh\n", func, line, r->len);
  295. DBG("%s:%d: offset %lxh\n", func, line, r->offset);
  296. }
  297. /**
  298. * dma_chunk - A chunk of dma pages mapped by the io controller.
  299. * @region - The dma region that owns this chunk.
  300. * @lpar_addr: Starting lpar address of the area to map.
  301. * @bus_addr: Starting ioc bus address of the area to map.
  302. * @len: Length in bytes of the area to map.
  303. * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the
  304. * list of all chuncks owned by the region.
  305. *
  306. * This implementation uses a very simple dma page manager
  307. * based on the dma_chunk structure. This scheme assumes
  308. * that all drivers use very well behaved dma ops.
  309. */
  310. struct dma_chunk {
  311. struct ps3_dma_region *region;
  312. unsigned long lpar_addr;
  313. unsigned long bus_addr;
  314. unsigned long len;
  315. struct list_head link;
  316. unsigned int usage_count;
  317. };
  318. #define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__)
  319. static void _dma_dump_chunk (const struct dma_chunk* c, const char* func,
  320. int line)
  321. {
  322. DBG("%s:%d: r.dev %llu:%llu\n", func, line,
  323. c->region->dev->bus_id, c->region->dev->dev_id);
  324. DBG("%s:%d: r.bus_addr %lxh\n", func, line, c->region->bus_addr);
  325. DBG("%s:%d: r.page_size %u\n", func, line, c->region->page_size);
  326. DBG("%s:%d: r.len %lxh\n", func, line, c->region->len);
  327. DBG("%s:%d: r.offset %lxh\n", func, line, c->region->offset);
  328. DBG("%s:%d: c.lpar_addr %lxh\n", func, line, c->lpar_addr);
  329. DBG("%s:%d: c.bus_addr %lxh\n", func, line, c->bus_addr);
  330. DBG("%s:%d: c.len %lxh\n", func, line, c->len);
  331. }
  332. static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r,
  333. unsigned long bus_addr, unsigned long len)
  334. {
  335. struct dma_chunk *c;
  336. unsigned long aligned_bus = _ALIGN_DOWN(bus_addr, 1 << r->page_size);
  337. unsigned long aligned_len = _ALIGN_UP(len+bus_addr-aligned_bus,
  338. 1 << r->page_size);
  339. list_for_each_entry(c, &r->chunk_list.head, link) {
  340. /* intersection */
  341. if (aligned_bus >= c->bus_addr &&
  342. aligned_bus + aligned_len <= c->bus_addr + c->len)
  343. return c;
  344. /* below */
  345. if (aligned_bus + aligned_len <= c->bus_addr)
  346. continue;
  347. /* above */
  348. if (aligned_bus >= c->bus_addr + c->len)
  349. continue;
  350. /* we don't handle the multi-chunk case for now */
  351. dma_dump_chunk(c);
  352. BUG();
  353. }
  354. return NULL;
  355. }
  356. static struct dma_chunk *dma_find_chunk_lpar(struct ps3_dma_region *r,
  357. unsigned long lpar_addr, unsigned long len)
  358. {
  359. struct dma_chunk *c;
  360. unsigned long aligned_lpar = _ALIGN_DOWN(lpar_addr, 1 << r->page_size);
  361. unsigned long aligned_len = _ALIGN_UP(len + lpar_addr - aligned_lpar,
  362. 1 << r->page_size);
  363. list_for_each_entry(c, &r->chunk_list.head, link) {
  364. /* intersection */
  365. if (c->lpar_addr <= aligned_lpar &&
  366. aligned_lpar < c->lpar_addr + c->len) {
  367. if (aligned_lpar + aligned_len <= c->lpar_addr + c->len)
  368. return c;
  369. else {
  370. dma_dump_chunk(c);
  371. BUG();
  372. }
  373. }
  374. /* below */
  375. if (aligned_lpar + aligned_len <= c->lpar_addr) {
  376. continue;
  377. }
  378. /* above */
  379. if (c->lpar_addr + c->len <= aligned_lpar) {
  380. continue;
  381. }
  382. }
  383. return NULL;
  384. }
  385. static int dma_sb_free_chunk(struct dma_chunk *c)
  386. {
  387. int result = 0;
  388. if (c->bus_addr) {
  389. result = lv1_unmap_device_dma_region(c->region->dev->bus_id,
  390. c->region->dev->dev_id, c->bus_addr, c->len);
  391. BUG_ON(result);
  392. }
  393. kfree(c);
  394. return result;
  395. }
  396. static int dma_ioc0_free_chunk(struct dma_chunk *c)
  397. {
  398. int result = 0;
  399. int iopage;
  400. unsigned long offset;
  401. struct ps3_dma_region *r = c->region;
  402. DBG("%s:start\n", __func__);
  403. for (iopage = 0; iopage < (c->len >> r->page_size); iopage++) {
  404. offset = (1 << r->page_size) * iopage;
  405. /* put INVALID entry */
  406. result = lv1_put_iopte(0,
  407. c->bus_addr + offset,
  408. c->lpar_addr + offset,
  409. r->ioid,
  410. 0);
  411. DBG("%s: bus=%#lx, lpar=%#lx, ioid=%d\n", __func__,
  412. c->bus_addr + offset,
  413. c->lpar_addr + offset,
  414. r->ioid);
  415. if (result) {
  416. DBG("%s:%d: lv1_put_iopte failed: %s\n", __func__,
  417. __LINE__, ps3_result(result));
  418. }
  419. }
  420. kfree(c);
  421. DBG("%s:end\n", __func__);
  422. return result;
  423. }
  424. /**
  425. * dma_sb_map_pages - Maps dma pages into the io controller bus address space.
  426. * @r: Pointer to a struct ps3_dma_region.
  427. * @phys_addr: Starting physical address of the area to map.
  428. * @len: Length in bytes of the area to map.
  429. * c_out: A pointer to receive an allocated struct dma_chunk for this area.
  430. *
  431. * This is the lowest level dma mapping routine, and is the one that will
  432. * make the HV call to add the pages into the io controller address space.
  433. */
  434. static int dma_sb_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
  435. unsigned long len, struct dma_chunk **c_out, u64 iopte_flag)
  436. {
  437. int result;
  438. struct dma_chunk *c;
  439. c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);
  440. if (!c) {
  441. result = -ENOMEM;
  442. goto fail_alloc;
  443. }
  444. c->region = r;
  445. c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
  446. c->bus_addr = dma_sb_lpar_to_bus(r, c->lpar_addr);
  447. c->len = len;
  448. BUG_ON(iopte_flag != 0xf800000000000000UL);
  449. result = lv1_map_device_dma_region(c->region->dev->bus_id,
  450. c->region->dev->dev_id, c->lpar_addr,
  451. c->bus_addr, c->len, iopte_flag);
  452. if (result) {
  453. DBG("%s:%d: lv1_map_device_dma_region failed: %s\n",
  454. __func__, __LINE__, ps3_result(result));
  455. goto fail_map;
  456. }
  457. list_add(&c->link, &r->chunk_list.head);
  458. *c_out = c;
  459. return 0;
  460. fail_map:
  461. kfree(c);
  462. fail_alloc:
  463. *c_out = NULL;
  464. DBG(" <- %s:%d\n", __func__, __LINE__);
  465. return result;
  466. }
  467. static int dma_ioc0_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
  468. unsigned long len, struct dma_chunk **c_out,
  469. u64 iopte_flag)
  470. {
  471. int result;
  472. struct dma_chunk *c, *last;
  473. int iopage, pages;
  474. unsigned long offset;
  475. DBG(KERN_ERR "%s: phy=%#lx, lpar%#lx, len=%#lx\n", __func__,
  476. phys_addr, ps3_mm_phys_to_lpar(phys_addr), len);
  477. c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);
  478. if (!c) {
  479. result = -ENOMEM;
  480. goto fail_alloc;
  481. }
  482. c->region = r;
  483. c->len = len;
  484. c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
  485. /* allocate IO address */
  486. if (list_empty(&r->chunk_list.head)) {
  487. /* first one */
  488. c->bus_addr = r->bus_addr;
  489. } else {
  490. /* derive from last bus addr*/
  491. last = list_entry(r->chunk_list.head.next,
  492. struct dma_chunk, link);
  493. c->bus_addr = last->bus_addr + last->len;
  494. DBG("%s: last bus=%#lx, len=%#lx\n", __func__,
  495. last->bus_addr, last->len);
  496. }
  497. /* FIXME: check whether length exceeds region size */
  498. /* build ioptes for the area */
  499. pages = len >> r->page_size;
  500. DBG("%s: pgsize=%#x len=%#lx pages=%#x iopteflag=%#llx\n", __func__,
  501. r->page_size, r->len, pages, iopte_flag);
  502. for (iopage = 0; iopage < pages; iopage++) {
  503. offset = (1 << r->page_size) * iopage;
  504. result = lv1_put_iopte(0,
  505. c->bus_addr + offset,
  506. c->lpar_addr + offset,
  507. r->ioid,
  508. iopte_flag);
  509. if (result) {
  510. printk(KERN_WARNING "%s:%d: lv1_map_device_dma_region "
  511. "failed: %s\n", __func__, __LINE__,
  512. 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. /* arrange to do this in ps3_mm_add_memory */
  1023. ps3_mm_region_create(&map.r1, map.total - map.rm.size);
  1024. /* correct map.total for the real total amount of memory we use */
  1025. map.total = map.rm.size + map.r1.size;
  1026. DBG(" <- %s:%d\n", __func__, __LINE__);
  1027. }
  1028. /**
  1029. * ps3_mm_shutdown - final cleanup of address space
  1030. */
  1031. void ps3_mm_shutdown(void)
  1032. {
  1033. ps3_mm_region_destroy(&map.r1);
  1034. }