mm.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  1. /*
  2. * PS3 address space management.
  3. *
  4. * Copyright (C) 2006 Sony Computer Entertainment Inc.
  5. * Copyright 2006 Sony Corp.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/memory_hotplug.h>
  23. #include <linux/lmb.h>
  24. #include <asm/firmware.h>
  25. #include <asm/prom.h>
  26. #include <asm/udbg.h>
  27. #include <asm/lv1call.h>
  28. #include "platform.h"
  29. #if defined(DEBUG)
  30. #define DBG udbg_printf
  31. #else
  32. #define DBG pr_debug
  33. #endif
  34. enum {
  35. #if defined(CONFIG_PS3_DYNAMIC_DMA)
  36. USE_DYNAMIC_DMA = 1,
  37. #else
  38. USE_DYNAMIC_DMA = 0,
  39. #endif
  40. };
  41. enum {
  42. PAGE_SHIFT_4K = 12U,
  43. PAGE_SHIFT_64K = 16U,
  44. PAGE_SHIFT_16M = 24U,
  45. };
  46. static unsigned long make_page_sizes(unsigned long a, unsigned long b)
  47. {
  48. return (a << 56) | (b << 48);
  49. }
  50. enum {
  51. ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04,
  52. ALLOCATE_MEMORY_ADDR_ZERO = 0X08,
  53. };
  54. /* valid htab sizes are {18,19,20} = 256K, 512K, 1M */
  55. enum {
  56. HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */
  57. HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */
  58. };
  59. /*============================================================================*/
  60. /* virtual address space routines */
  61. /*============================================================================*/
  62. /**
  63. * struct mem_region - memory region structure
  64. * @base: base address
  65. * @size: size in bytes
  66. * @offset: difference between base and rm.size
  67. */
  68. struct mem_region {
  69. unsigned long base;
  70. unsigned long size;
  71. unsigned long offset;
  72. };
  73. /**
  74. * struct map - address space state variables holder
  75. * @total: total memory available as reported by HV
  76. * @vas_id - HV virtual address space id
  77. * @htab_size: htab size in bytes
  78. *
  79. * The HV virtual address space (vas) allows for hotplug memory regions.
  80. * Memory regions can be created and destroyed in the vas at runtime.
  81. * @rm: real mode (bootmem) region
  82. * @r1: hotplug memory region(s)
  83. *
  84. * ps3 addresses
  85. * virt_addr: a cpu 'translated' effective address
  86. * phys_addr: an address in what Linux thinks is the physical address space
  87. * lpar_addr: an address in the HV virtual address space
  88. * bus_addr: an io controller 'translated' address on a device bus
  89. */
  90. struct map {
  91. unsigned long total;
  92. unsigned long vas_id;
  93. unsigned long htab_size;
  94. struct mem_region rm;
  95. struct mem_region r1;
  96. };
  97. #define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__)
  98. static void __maybe_unused _debug_dump_map(const struct map *m,
  99. const char *func, int line)
  100. {
  101. DBG("%s:%d: map.total = %lxh\n", func, line, m->total);
  102. DBG("%s:%d: map.rm.size = %lxh\n", func, line, m->rm.size);
  103. DBG("%s:%d: map.vas_id = %lu\n", func, line, m->vas_id);
  104. DBG("%s:%d: map.htab_size = %lxh\n", func, line, m->htab_size);
  105. DBG("%s:%d: map.r1.base = %lxh\n", func, line, m->r1.base);
  106. DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset);
  107. DBG("%s:%d: map.r1.size = %lxh\n", func, line, m->r1.size);
  108. }
  109. static struct map map;
  110. /**
  111. * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address
  112. * @phys_addr: linux physical address
  113. */
  114. unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr)
  115. {
  116. BUG_ON(is_kernel_addr(phys_addr));
  117. return (phys_addr < map.rm.size || phys_addr >= map.total)
  118. ? phys_addr : phys_addr + map.r1.offset;
  119. }
  120. EXPORT_SYMBOL(ps3_mm_phys_to_lpar);
  121. /**
  122. * ps3_mm_vas_create - create the virtual address space
  123. */
  124. void __init ps3_mm_vas_create(unsigned long* htab_size)
  125. {
  126. int result;
  127. unsigned long start_address;
  128. unsigned long size;
  129. unsigned long access_right;
  130. unsigned long max_page_size;
  131. unsigned long flags;
  132. result = lv1_query_logical_partition_address_region_info(0,
  133. &start_address, &size, &access_right, &max_page_size,
  134. &flags);
  135. if (result) {
  136. DBG("%s:%d: lv1_query_logical_partition_address_region_info "
  137. "failed: %s\n", __func__, __LINE__,
  138. ps3_result(result));
  139. goto fail;
  140. }
  141. if (max_page_size < PAGE_SHIFT_16M) {
  142. DBG("%s:%d: bad max_page_size %lxh\n", __func__, __LINE__,
  143. max_page_size);
  144. goto fail;
  145. }
  146. BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE > HTAB_SIZE_MAX);
  147. BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE < HTAB_SIZE_MIN);
  148. result = lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE,
  149. 2, make_page_sizes(PAGE_SHIFT_16M, PAGE_SHIFT_64K),
  150. &map.vas_id, &map.htab_size);
  151. if (result) {
  152. DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n",
  153. __func__, __LINE__, ps3_result(result));
  154. goto fail;
  155. }
  156. result = lv1_select_virtual_address_space(map.vas_id);
  157. if (result) {
  158. DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n",
  159. __func__, __LINE__, ps3_result(result));
  160. goto fail;
  161. }
  162. *htab_size = map.htab_size;
  163. debug_dump_map(&map);
  164. return;
  165. fail:
  166. panic("ps3_mm_vas_create failed");
  167. }
  168. /**
  169. * ps3_mm_vas_destroy -
  170. */
  171. void ps3_mm_vas_destroy(void)
  172. {
  173. int result;
  174. DBG("%s:%d: map.vas_id = %lu\n", __func__, __LINE__, map.vas_id);
  175. if (map.vas_id) {
  176. result = lv1_select_virtual_address_space(0);
  177. BUG_ON(result);
  178. result = lv1_destruct_virtual_address_space(map.vas_id);
  179. BUG_ON(result);
  180. map.vas_id = 0;
  181. }
  182. }
  183. /*============================================================================*/
  184. /* memory hotplug routines */
  185. /*============================================================================*/
  186. /**
  187. * ps3_mm_region_create - create a memory region in the vas
  188. * @r: pointer to a struct mem_region to accept initialized values
  189. * @size: requested region size
  190. *
  191. * This implementation creates the region with the vas large page size.
  192. * @size is rounded down to a multiple of the vas large page size.
  193. */
  194. static int ps3_mm_region_create(struct mem_region *r, unsigned long size)
  195. {
  196. int result;
  197. unsigned long muid;
  198. r->size = _ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M);
  199. DBG("%s:%d requested %lxh\n", __func__, __LINE__, size);
  200. DBG("%s:%d actual %lxh\n", __func__, __LINE__, r->size);
  201. DBG("%s:%d difference %lxh (%luMB)\n", __func__, __LINE__,
  202. (unsigned long)(size - r->size),
  203. (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 = %lxh\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. DBG("%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. DBG("%s:%d: online_pages failed: (%d)\n",
  265. __func__, __LINE__, result);
  266. return result;
  267. }
  268. core_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 %lu:%lu\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 %lu:%lu\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=%#lx\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. pr_info(" -> %s:%d:\n", __func__, __LINE__);
  548. BUG_ON(!r);
  549. if (!r->dev->bus_id) {
  550. pr_info("%s:%d: %lu:%lu 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. &r->bus_addr);
  564. if (result) {
  565. DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n",
  566. __func__, __LINE__, ps3_result(result));
  567. r->len = r->bus_addr = 0;
  568. }
  569. return result;
  570. }
  571. static int dma_ioc0_region_create(struct ps3_dma_region *r)
  572. {
  573. int result;
  574. INIT_LIST_HEAD(&r->chunk_list.head);
  575. spin_lock_init(&r->chunk_list.lock);
  576. result = lv1_allocate_io_segment(0,
  577. r->len,
  578. r->page_size,
  579. &r->bus_addr);
  580. if (result) {
  581. DBG("%s:%d: lv1_allocate_io_segment failed: %s\n",
  582. __func__, __LINE__, ps3_result(result));
  583. r->len = r->bus_addr = 0;
  584. }
  585. DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__,
  586. r->len, r->page_size, r->bus_addr);
  587. return result;
  588. }
  589. /**
  590. * dma_region_free - Free a device dma region.
  591. * @r: Pointer to a struct ps3_dma_region.
  592. *
  593. * This is the lowest level dma region free routine, and is the one that
  594. * will make the HV call to free the region.
  595. */
  596. static int dma_sb_region_free(struct ps3_dma_region *r)
  597. {
  598. int result;
  599. struct dma_chunk *c;
  600. struct dma_chunk *tmp;
  601. BUG_ON(!r);
  602. if (!r->dev->bus_id) {
  603. pr_info("%s:%d: %lu:%lu no dma\n", __func__, __LINE__,
  604. r->dev->bus_id, r->dev->dev_id);
  605. return 0;
  606. }
  607. list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) {
  608. list_del(&c->link);
  609. dma_sb_free_chunk(c);
  610. }
  611. result = lv1_free_device_dma_region(r->dev->bus_id, r->dev->dev_id,
  612. r->bus_addr);
  613. if (result)
  614. DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
  615. __func__, __LINE__, ps3_result(result));
  616. r->bus_addr = 0;
  617. return result;
  618. }
  619. static int dma_ioc0_region_free(struct ps3_dma_region *r)
  620. {
  621. int result;
  622. struct dma_chunk *c, *n;
  623. DBG("%s: start\n", __func__);
  624. list_for_each_entry_safe(c, n, &r->chunk_list.head, link) {
  625. list_del(&c->link);
  626. dma_ioc0_free_chunk(c);
  627. }
  628. result = lv1_release_io_segment(0, r->bus_addr);
  629. if (result)
  630. DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
  631. __func__, __LINE__, ps3_result(result));
  632. r->bus_addr = 0;
  633. DBG("%s: end\n", __func__);
  634. return result;
  635. }
  636. /**
  637. * dma_sb_map_area - Map an area of memory into a device dma region.
  638. * @r: Pointer to a struct ps3_dma_region.
  639. * @virt_addr: Starting virtual address of the area to map.
  640. * @len: Length in bytes of the area to map.
  641. * @bus_addr: A pointer to return the starting ioc bus address of the area to
  642. * map.
  643. *
  644. * This is the common dma mapping routine.
  645. */
  646. static int dma_sb_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
  647. unsigned long len, unsigned long *bus_addr,
  648. u64 iopte_flag)
  649. {
  650. int result;
  651. unsigned long flags;
  652. struct dma_chunk *c;
  653. unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
  654. : virt_addr;
  655. unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
  656. unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
  657. 1 << r->page_size);
  658. *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
  659. if (!USE_DYNAMIC_DMA) {
  660. unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
  661. DBG(" -> %s:%d\n", __func__, __LINE__);
  662. DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__,
  663. virt_addr);
  664. DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__,
  665. phys_addr);
  666. DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__,
  667. lpar_addr);
  668. DBG("%s:%d len %lxh\n", __func__, __LINE__, len);
  669. DBG("%s:%d bus_addr %lxh (%lxh)\n", __func__, __LINE__,
  670. *bus_addr, len);
  671. }
  672. spin_lock_irqsave(&r->chunk_list.lock, flags);
  673. c = dma_find_chunk(r, *bus_addr, len);
  674. if (c) {
  675. DBG("%s:%d: reusing mapped chunk", __func__, __LINE__);
  676. dma_dump_chunk(c);
  677. c->usage_count++;
  678. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  679. return 0;
  680. }
  681. result = dma_sb_map_pages(r, aligned_phys, aligned_len, &c, iopte_flag);
  682. if (result) {
  683. *bus_addr = 0;
  684. DBG("%s:%d: dma_sb_map_pages failed (%d)\n",
  685. __func__, __LINE__, result);
  686. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  687. return result;
  688. }
  689. c->usage_count = 1;
  690. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  691. return result;
  692. }
  693. static int dma_ioc0_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
  694. unsigned long len, unsigned long *bus_addr,
  695. u64 iopte_flag)
  696. {
  697. int result;
  698. unsigned long flags;
  699. struct dma_chunk *c;
  700. unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
  701. : virt_addr;
  702. unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
  703. unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
  704. 1 << r->page_size);
  705. DBG(KERN_ERR "%s: vaddr=%#lx, len=%#lx\n", __func__,
  706. virt_addr, len);
  707. DBG(KERN_ERR "%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__,
  708. phys_addr, aligned_phys, aligned_len);
  709. spin_lock_irqsave(&r->chunk_list.lock, flags);
  710. c = dma_find_chunk_lpar(r, ps3_mm_phys_to_lpar(phys_addr), len);
  711. if (c) {
  712. /* FIXME */
  713. BUG();
  714. *bus_addr = c->bus_addr + phys_addr - aligned_phys;
  715. c->usage_count++;
  716. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  717. return 0;
  718. }
  719. result = dma_ioc0_map_pages(r, aligned_phys, aligned_len, &c,
  720. iopte_flag);
  721. if (result) {
  722. *bus_addr = 0;
  723. DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n",
  724. __func__, __LINE__, result);
  725. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  726. return result;
  727. }
  728. *bus_addr = c->bus_addr + phys_addr - aligned_phys;
  729. DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#lx\n", __func__,
  730. virt_addr, phys_addr, aligned_phys, *bus_addr);
  731. c->usage_count = 1;
  732. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  733. return result;
  734. }
  735. /**
  736. * dma_sb_unmap_area - Unmap an area of memory from a device dma region.
  737. * @r: Pointer to a struct ps3_dma_region.
  738. * @bus_addr: The starting ioc bus address of the area to unmap.
  739. * @len: Length in bytes of the area to unmap.
  740. *
  741. * This is the common dma unmap routine.
  742. */
  743. static int dma_sb_unmap_area(struct ps3_dma_region *r, unsigned long bus_addr,
  744. unsigned long len)
  745. {
  746. unsigned long flags;
  747. struct dma_chunk *c;
  748. spin_lock_irqsave(&r->chunk_list.lock, flags);
  749. c = dma_find_chunk(r, bus_addr, len);
  750. if (!c) {
  751. unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
  752. 1 << r->page_size);
  753. unsigned long aligned_len = _ALIGN_UP(len + bus_addr
  754. - aligned_bus, 1 << r->page_size);
  755. DBG("%s:%d: not found: bus_addr %lxh\n",
  756. __func__, __LINE__, bus_addr);
  757. DBG("%s:%d: not found: len %lxh\n",
  758. __func__, __LINE__, len);
  759. DBG("%s:%d: not found: aligned_bus %lxh\n",
  760. __func__, __LINE__, aligned_bus);
  761. DBG("%s:%d: not found: aligned_len %lxh\n",
  762. __func__, __LINE__, aligned_len);
  763. BUG();
  764. }
  765. c->usage_count--;
  766. if (!c->usage_count) {
  767. list_del(&c->link);
  768. dma_sb_free_chunk(c);
  769. }
  770. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  771. return 0;
  772. }
  773. static int dma_ioc0_unmap_area(struct ps3_dma_region *r,
  774. unsigned long bus_addr, unsigned long len)
  775. {
  776. unsigned long flags;
  777. struct dma_chunk *c;
  778. DBG("%s: start a=%#lx l=%#lx\n", __func__, bus_addr, len);
  779. spin_lock_irqsave(&r->chunk_list.lock, flags);
  780. c = dma_find_chunk(r, bus_addr, len);
  781. if (!c) {
  782. unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
  783. 1 << r->page_size);
  784. unsigned long aligned_len = _ALIGN_UP(len + bus_addr
  785. - aligned_bus,
  786. 1 << r->page_size);
  787. DBG("%s:%d: not found: bus_addr %lxh\n",
  788. __func__, __LINE__, bus_addr);
  789. DBG("%s:%d: not found: len %lxh\n",
  790. __func__, __LINE__, len);
  791. DBG("%s:%d: not found: aligned_bus %lxh\n",
  792. __func__, __LINE__, aligned_bus);
  793. DBG("%s:%d: not found: aligned_len %lxh\n",
  794. __func__, __LINE__, aligned_len);
  795. BUG();
  796. }
  797. c->usage_count--;
  798. if (!c->usage_count) {
  799. list_del(&c->link);
  800. dma_ioc0_free_chunk(c);
  801. }
  802. spin_unlock_irqrestore(&r->chunk_list.lock, flags);
  803. DBG("%s: end\n", __func__);
  804. return 0;
  805. }
  806. /**
  807. * dma_sb_region_create_linear - Setup a linear dma mapping for a device.
  808. * @r: Pointer to a struct ps3_dma_region.
  809. *
  810. * This routine creates an HV dma region for the device and maps all available
  811. * ram into the io controller bus address space.
  812. */
  813. static int dma_sb_region_create_linear(struct ps3_dma_region *r)
  814. {
  815. int result;
  816. unsigned long virt_addr, len, tmp;
  817. if (r->len > 16*1024*1024) { /* FIXME: need proper fix */
  818. /* force 16M dma pages for linear mapping */
  819. if (r->page_size != PS3_DMA_16M) {
  820. pr_info("%s:%d: forcing 16M pages for linear map\n",
  821. __func__, __LINE__);
  822. r->page_size = PS3_DMA_16M;
  823. r->len = _ALIGN_UP(r->len, 1 << r->page_size);
  824. }
  825. }
  826. result = dma_sb_region_create(r);
  827. BUG_ON(result);
  828. if (r->offset < map.rm.size) {
  829. /* Map (part of) 1st RAM chunk */
  830. virt_addr = map.rm.base + r->offset;
  831. len = map.rm.size - r->offset;
  832. if (len > r->len)
  833. len = r->len;
  834. result = dma_sb_map_area(r, virt_addr, len, &tmp,
  835. IOPTE_PP_W | IOPTE_PP_R | IOPTE_SO_RW | IOPTE_M);
  836. BUG_ON(result);
  837. }
  838. if (r->offset + r->len > map.rm.size) {
  839. /* Map (part of) 2nd RAM chunk */
  840. virt_addr = map.rm.size;
  841. len = r->len;
  842. if (r->offset >= map.rm.size)
  843. virt_addr += r->offset - map.rm.size;
  844. else
  845. len -= map.rm.size - r->offset;
  846. result = dma_sb_map_area(r, virt_addr, len, &tmp,
  847. IOPTE_PP_W | IOPTE_PP_R | IOPTE_SO_RW | IOPTE_M);
  848. BUG_ON(result);
  849. }
  850. return result;
  851. }
  852. /**
  853. * dma_sb_region_free_linear - Free a linear dma mapping for a device.
  854. * @r: Pointer to a struct ps3_dma_region.
  855. *
  856. * This routine will unmap all mapped areas and free the HV dma region.
  857. */
  858. static int dma_sb_region_free_linear(struct ps3_dma_region *r)
  859. {
  860. int result;
  861. unsigned long bus_addr, len, lpar_addr;
  862. if (r->offset < map.rm.size) {
  863. /* Unmap (part of) 1st RAM chunk */
  864. lpar_addr = map.rm.base + r->offset;
  865. len = map.rm.size - r->offset;
  866. if (len > r->len)
  867. len = r->len;
  868. bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
  869. result = dma_sb_unmap_area(r, bus_addr, len);
  870. BUG_ON(result);
  871. }
  872. if (r->offset + r->len > map.rm.size) {
  873. /* Unmap (part of) 2nd RAM chunk */
  874. lpar_addr = map.r1.base;
  875. len = r->len;
  876. if (r->offset >= map.rm.size)
  877. lpar_addr += r->offset - map.rm.size;
  878. else
  879. len -= map.rm.size - r->offset;
  880. bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
  881. result = dma_sb_unmap_area(r, bus_addr, len);
  882. BUG_ON(result);
  883. }
  884. result = dma_sb_region_free(r);
  885. BUG_ON(result);
  886. return result;
  887. }
  888. /**
  889. * dma_sb_map_area_linear - Map an area of memory into a device dma region.
  890. * @r: Pointer to a struct ps3_dma_region.
  891. * @virt_addr: Starting virtual address of the area to map.
  892. * @len: Length in bytes of the area to map.
  893. * @bus_addr: A pointer to return the starting ioc bus address of the area to
  894. * map.
  895. *
  896. * This routine just returns the corresponding bus address. Actual mapping
  897. * occurs in dma_region_create_linear().
  898. */
  899. static int dma_sb_map_area_linear(struct ps3_dma_region *r,
  900. unsigned long virt_addr, unsigned long len, unsigned long *bus_addr,
  901. u64 iopte_flag)
  902. {
  903. unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
  904. : virt_addr;
  905. *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
  906. return 0;
  907. }
  908. /**
  909. * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
  910. * @r: Pointer to a struct ps3_dma_region.
  911. * @bus_addr: The starting ioc bus address of the area to unmap.
  912. * @len: Length in bytes of the area to unmap.
  913. *
  914. * This routine does nothing. Unmapping occurs in dma_sb_region_free_linear().
  915. */
  916. static int dma_sb_unmap_area_linear(struct ps3_dma_region *r,
  917. unsigned long bus_addr, unsigned long len)
  918. {
  919. return 0;
  920. };
  921. static const struct ps3_dma_region_ops ps3_dma_sb_region_ops = {
  922. .create = dma_sb_region_create,
  923. .free = dma_sb_region_free,
  924. .map = dma_sb_map_area,
  925. .unmap = dma_sb_unmap_area
  926. };
  927. static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = {
  928. .create = dma_sb_region_create_linear,
  929. .free = dma_sb_region_free_linear,
  930. .map = dma_sb_map_area_linear,
  931. .unmap = dma_sb_unmap_area_linear
  932. };
  933. static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = {
  934. .create = dma_ioc0_region_create,
  935. .free = dma_ioc0_region_free,
  936. .map = dma_ioc0_map_area,
  937. .unmap = dma_ioc0_unmap_area
  938. };
  939. int ps3_dma_region_init(struct ps3_system_bus_device *dev,
  940. struct ps3_dma_region *r, enum ps3_dma_page_size page_size,
  941. enum ps3_dma_region_type region_type, void *addr, unsigned long len)
  942. {
  943. unsigned long lpar_addr;
  944. lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0;
  945. r->dev = dev;
  946. r->page_size = page_size;
  947. r->region_type = region_type;
  948. r->offset = lpar_addr;
  949. if (r->offset >= map.rm.size)
  950. r->offset -= map.r1.offset;
  951. r->len = len ? len : _ALIGN_UP(map.total, 1 << r->page_size);
  952. switch (dev->dev_type) {
  953. case PS3_DEVICE_TYPE_SB:
  954. r->region_ops = (USE_DYNAMIC_DMA)
  955. ? &ps3_dma_sb_region_ops
  956. : &ps3_dma_sb_region_linear_ops;
  957. break;
  958. case PS3_DEVICE_TYPE_IOC0:
  959. r->region_ops = &ps3_dma_ioc0_region_ops;
  960. break;
  961. default:
  962. BUG();
  963. return -EINVAL;
  964. }
  965. return 0;
  966. }
  967. EXPORT_SYMBOL(ps3_dma_region_init);
  968. int ps3_dma_region_create(struct ps3_dma_region *r)
  969. {
  970. BUG_ON(!r);
  971. BUG_ON(!r->region_ops);
  972. BUG_ON(!r->region_ops->create);
  973. return r->region_ops->create(r);
  974. }
  975. EXPORT_SYMBOL(ps3_dma_region_create);
  976. int ps3_dma_region_free(struct ps3_dma_region *r)
  977. {
  978. BUG_ON(!r);
  979. BUG_ON(!r->region_ops);
  980. BUG_ON(!r->region_ops->free);
  981. return r->region_ops->free(r);
  982. }
  983. EXPORT_SYMBOL(ps3_dma_region_free);
  984. int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr,
  985. unsigned long len, unsigned long *bus_addr,
  986. u64 iopte_flag)
  987. {
  988. return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag);
  989. }
  990. int ps3_dma_unmap(struct ps3_dma_region *r, unsigned long bus_addr,
  991. unsigned long len)
  992. {
  993. return r->region_ops->unmap(r, bus_addr, len);
  994. }
  995. /*============================================================================*/
  996. /* system startup routines */
  997. /*============================================================================*/
  998. /**
  999. * ps3_mm_init - initialize the address space state variables
  1000. */
  1001. void __init ps3_mm_init(void)
  1002. {
  1003. int result;
  1004. DBG(" -> %s:%d\n", __func__, __LINE__);
  1005. result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size,
  1006. &map.total);
  1007. if (result)
  1008. panic("ps3_repository_read_mm_info() failed");
  1009. map.rm.offset = map.rm.base;
  1010. map.vas_id = map.htab_size = 0;
  1011. /* this implementation assumes map.rm.base is zero */
  1012. BUG_ON(map.rm.base);
  1013. BUG_ON(!map.rm.size);
  1014. /* arrange to do this in ps3_mm_add_memory */
  1015. ps3_mm_region_create(&map.r1, map.total - map.rm.size);
  1016. /* correct map.total for the real total amount of memory we use */
  1017. map.total = map.rm.size + map.r1.size;
  1018. DBG(" <- %s:%d\n", __func__, __LINE__);
  1019. }
  1020. /**
  1021. * ps3_mm_shutdown - final cleanup of address space
  1022. */
  1023. void ps3_mm_shutdown(void)
  1024. {
  1025. ps3_mm_region_destroy(&map.r1);
  1026. }