mm.c 32 KB

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