mm.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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/cell-regs.h>
  25. #include <asm/firmware.h>
  26. #include <asm/prom.h>
  27. #include <asm/udbg.h>
  28. #include <asm/lv1call.h>
  29. #include "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. pr_warning("%s:%d: lv1_put_iopte failed: %s\n",
  511. __func__, __LINE__, 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. CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
  840. CBE_IOPTE_M);
  841. BUG_ON(result);
  842. }
  843. if (r->offset + r->len > map.rm.size) {
  844. /* Map (part of) 2nd RAM chunk */
  845. virt_addr = map.rm.size;
  846. len = r->len;
  847. if (r->offset >= map.rm.size)
  848. virt_addr += r->offset - map.rm.size;
  849. else
  850. len -= map.rm.size - r->offset;
  851. result = dma_sb_map_area(r, virt_addr, len, &tmp,
  852. CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
  853. CBE_IOPTE_M);
  854. BUG_ON(result);
  855. }
  856. return result;
  857. }
  858. /**
  859. * dma_sb_region_free_linear - Free a linear dma mapping for a device.
  860. * @r: Pointer to a struct ps3_dma_region.
  861. *
  862. * This routine will unmap all mapped areas and free the HV dma region.
  863. */
  864. static int dma_sb_region_free_linear(struct ps3_dma_region *r)
  865. {
  866. int result;
  867. dma_addr_t bus_addr;
  868. unsigned long len, lpar_addr;
  869. if (r->offset < map.rm.size) {
  870. /* Unmap (part of) 1st RAM chunk */
  871. lpar_addr = map.rm.base + r->offset;
  872. len = map.rm.size - r->offset;
  873. if (len > r->len)
  874. len = r->len;
  875. bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
  876. result = dma_sb_unmap_area(r, bus_addr, len);
  877. BUG_ON(result);
  878. }
  879. if (r->offset + r->len > map.rm.size) {
  880. /* Unmap (part of) 2nd RAM chunk */
  881. lpar_addr = map.r1.base;
  882. len = r->len;
  883. if (r->offset >= map.rm.size)
  884. lpar_addr += r->offset - map.rm.size;
  885. else
  886. len -= map.rm.size - r->offset;
  887. bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
  888. result = dma_sb_unmap_area(r, bus_addr, len);
  889. BUG_ON(result);
  890. }
  891. result = dma_sb_region_free(r);
  892. BUG_ON(result);
  893. return result;
  894. }
  895. /**
  896. * dma_sb_map_area_linear - Map an area of memory into a device dma region.
  897. * @r: Pointer to a struct ps3_dma_region.
  898. * @virt_addr: Starting virtual address of the area to map.
  899. * @len: Length in bytes of the area to map.
  900. * @bus_addr: A pointer to return the starting ioc bus address of the area to
  901. * map.
  902. *
  903. * This routine just returns the corresponding bus address. Actual mapping
  904. * occurs in dma_region_create_linear().
  905. */
  906. static int dma_sb_map_area_linear(struct ps3_dma_region *r,
  907. unsigned long virt_addr, unsigned long len, dma_addr_t *bus_addr,
  908. u64 iopte_flag)
  909. {
  910. unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
  911. : virt_addr;
  912. *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
  913. return 0;
  914. }
  915. /**
  916. * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
  917. * @r: Pointer to a struct ps3_dma_region.
  918. * @bus_addr: The starting ioc bus address of the area to unmap.
  919. * @len: Length in bytes of the area to unmap.
  920. *
  921. * This routine does nothing. Unmapping occurs in dma_sb_region_free_linear().
  922. */
  923. static int dma_sb_unmap_area_linear(struct ps3_dma_region *r,
  924. dma_addr_t bus_addr, unsigned long len)
  925. {
  926. return 0;
  927. };
  928. static const struct ps3_dma_region_ops ps3_dma_sb_region_ops = {
  929. .create = dma_sb_region_create,
  930. .free = dma_sb_region_free,
  931. .map = dma_sb_map_area,
  932. .unmap = dma_sb_unmap_area
  933. };
  934. static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = {
  935. .create = dma_sb_region_create_linear,
  936. .free = dma_sb_region_free_linear,
  937. .map = dma_sb_map_area_linear,
  938. .unmap = dma_sb_unmap_area_linear
  939. };
  940. static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = {
  941. .create = dma_ioc0_region_create,
  942. .free = dma_ioc0_region_free,
  943. .map = dma_ioc0_map_area,
  944. .unmap = dma_ioc0_unmap_area
  945. };
  946. int ps3_dma_region_init(struct ps3_system_bus_device *dev,
  947. struct ps3_dma_region *r, enum ps3_dma_page_size page_size,
  948. enum ps3_dma_region_type region_type, void *addr, unsigned long len)
  949. {
  950. unsigned long lpar_addr;
  951. lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0;
  952. r->dev = dev;
  953. r->page_size = page_size;
  954. r->region_type = region_type;
  955. r->offset = lpar_addr;
  956. if (r->offset >= map.rm.size)
  957. r->offset -= map.r1.offset;
  958. r->len = len ? len : _ALIGN_UP(map.total, 1 << r->page_size);
  959. switch (dev->dev_type) {
  960. case PS3_DEVICE_TYPE_SB:
  961. r->region_ops = (USE_DYNAMIC_DMA)
  962. ? &ps3_dma_sb_region_ops
  963. : &ps3_dma_sb_region_linear_ops;
  964. break;
  965. case PS3_DEVICE_TYPE_IOC0:
  966. r->region_ops = &ps3_dma_ioc0_region_ops;
  967. break;
  968. default:
  969. BUG();
  970. return -EINVAL;
  971. }
  972. return 0;
  973. }
  974. EXPORT_SYMBOL(ps3_dma_region_init);
  975. int ps3_dma_region_create(struct ps3_dma_region *r)
  976. {
  977. BUG_ON(!r);
  978. BUG_ON(!r->region_ops);
  979. BUG_ON(!r->region_ops->create);
  980. return r->region_ops->create(r);
  981. }
  982. EXPORT_SYMBOL(ps3_dma_region_create);
  983. int ps3_dma_region_free(struct ps3_dma_region *r)
  984. {
  985. BUG_ON(!r);
  986. BUG_ON(!r->region_ops);
  987. BUG_ON(!r->region_ops->free);
  988. return r->region_ops->free(r);
  989. }
  990. EXPORT_SYMBOL(ps3_dma_region_free);
  991. int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr,
  992. unsigned long len, dma_addr_t *bus_addr,
  993. u64 iopte_flag)
  994. {
  995. return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag);
  996. }
  997. int ps3_dma_unmap(struct ps3_dma_region *r, dma_addr_t bus_addr,
  998. unsigned long len)
  999. {
  1000. return r->region_ops->unmap(r, bus_addr, len);
  1001. }
  1002. /*============================================================================*/
  1003. /* system startup routines */
  1004. /*============================================================================*/
  1005. /**
  1006. * ps3_mm_init - initialize the address space state variables
  1007. */
  1008. void __init ps3_mm_init(void)
  1009. {
  1010. int result;
  1011. DBG(" -> %s:%d\n", __func__, __LINE__);
  1012. result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size,
  1013. &map.total);
  1014. if (result)
  1015. panic("ps3_repository_read_mm_info() failed");
  1016. map.rm.offset = map.rm.base;
  1017. map.vas_id = map.htab_size = 0;
  1018. /* this implementation assumes map.rm.base is zero */
  1019. BUG_ON(map.rm.base);
  1020. BUG_ON(!map.rm.size);
  1021. /* arrange to do this in ps3_mm_add_memory */
  1022. ps3_mm_region_create(&map.r1, map.total - map.rm.size);
  1023. /* correct map.total for the real total amount of memory we use */
  1024. map.total = map.rm.size + map.r1.size;
  1025. DBG(" <- %s:%d\n", __func__, __LINE__);
  1026. }
  1027. /**
  1028. * ps3_mm_shutdown - final cleanup of address space
  1029. */
  1030. void ps3_mm_shutdown(void)
  1031. {
  1032. ps3_mm_region_destroy(&map.r1);
  1033. }