mmu.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /**************************************************************************
  2. * Copyright (c) 2007, Intel Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. **************************************************************************/
  18. #include <drm/drmP.h>
  19. #include "psb_drv.h"
  20. #include "psb_reg.h"
  21. #include "mmu.h"
  22. /*
  23. * Code for the SGX MMU:
  24. */
  25. /*
  26. * clflush on one processor only:
  27. * clflush should apparently flush the cache line on all processors in an
  28. * SMP system.
  29. */
  30. /*
  31. * kmap atomic:
  32. * The usage of the slots must be completely encapsulated within a spinlock, and
  33. * no other functions that may be using the locks for other purposed may be
  34. * called from within the locked region.
  35. * Since the slots are per processor, this will guarantee that we are the only
  36. * user.
  37. */
  38. /*
  39. * TODO: Inserting ptes from an interrupt handler:
  40. * This may be desirable for some SGX functionality where the GPU can fault in
  41. * needed pages. For that, we need to make an atomic insert_pages function, that
  42. * may fail.
  43. * If it fails, the caller need to insert the page using a workqueue function,
  44. * but on average it should be fast.
  45. */
  46. static inline uint32_t psb_mmu_pt_index(uint32_t offset)
  47. {
  48. return (offset >> PSB_PTE_SHIFT) & 0x3FF;
  49. }
  50. static inline uint32_t psb_mmu_pd_index(uint32_t offset)
  51. {
  52. return offset >> PSB_PDE_SHIFT;
  53. }
  54. #if defined(CONFIG_X86)
  55. static inline void psb_clflush(void *addr)
  56. {
  57. __asm__ __volatile__("clflush (%0)\n" : : "r"(addr) : "memory");
  58. }
  59. static inline void psb_mmu_clflush(struct psb_mmu_driver *driver, void *addr)
  60. {
  61. if (!driver->has_clflush)
  62. return;
  63. mb();
  64. psb_clflush(addr);
  65. mb();
  66. }
  67. #else
  68. static inline void psb_mmu_clflush(struct psb_mmu_driver *driver, void *addr)
  69. {;
  70. }
  71. #endif
  72. static void psb_mmu_flush_pd_locked(struct psb_mmu_driver *driver, int force)
  73. {
  74. struct drm_device *dev = driver->dev;
  75. struct drm_psb_private *dev_priv = dev->dev_private;
  76. if (atomic_read(&driver->needs_tlbflush) || force) {
  77. uint32_t val = PSB_RSGX32(PSB_CR_BIF_CTRL);
  78. PSB_WSGX32(val | _PSB_CB_CTRL_INVALDC, PSB_CR_BIF_CTRL);
  79. /* Make sure data cache is turned off before enabling it */
  80. wmb();
  81. PSB_WSGX32(val & ~_PSB_CB_CTRL_INVALDC, PSB_CR_BIF_CTRL);
  82. (void)PSB_RSGX32(PSB_CR_BIF_CTRL);
  83. if (driver->msvdx_mmu_invaldc)
  84. atomic_set(driver->msvdx_mmu_invaldc, 1);
  85. }
  86. atomic_set(&driver->needs_tlbflush, 0);
  87. }
  88. #if 0
  89. static void psb_mmu_flush_pd(struct psb_mmu_driver *driver, int force)
  90. {
  91. down_write(&driver->sem);
  92. psb_mmu_flush_pd_locked(driver, force);
  93. up_write(&driver->sem);
  94. }
  95. #endif
  96. void psb_mmu_flush(struct psb_mmu_driver *driver)
  97. {
  98. struct drm_device *dev = driver->dev;
  99. struct drm_psb_private *dev_priv = dev->dev_private;
  100. uint32_t val;
  101. down_write(&driver->sem);
  102. val = PSB_RSGX32(PSB_CR_BIF_CTRL);
  103. if (atomic_read(&driver->needs_tlbflush))
  104. PSB_WSGX32(val | _PSB_CB_CTRL_INVALDC, PSB_CR_BIF_CTRL);
  105. else
  106. PSB_WSGX32(val | _PSB_CB_CTRL_FLUSH, PSB_CR_BIF_CTRL);
  107. /* Make sure data cache is turned off and MMU is flushed before
  108. restoring bank interface control register */
  109. wmb();
  110. PSB_WSGX32(val & ~(_PSB_CB_CTRL_FLUSH | _PSB_CB_CTRL_INVALDC),
  111. PSB_CR_BIF_CTRL);
  112. (void)PSB_RSGX32(PSB_CR_BIF_CTRL);
  113. atomic_set(&driver->needs_tlbflush, 0);
  114. if (driver->msvdx_mmu_invaldc)
  115. atomic_set(driver->msvdx_mmu_invaldc, 1);
  116. up_write(&driver->sem);
  117. }
  118. void psb_mmu_set_pd_context(struct psb_mmu_pd *pd, int hw_context)
  119. {
  120. struct drm_device *dev = pd->driver->dev;
  121. struct drm_psb_private *dev_priv = dev->dev_private;
  122. uint32_t offset = (hw_context == 0) ? PSB_CR_BIF_DIR_LIST_BASE0 :
  123. PSB_CR_BIF_DIR_LIST_BASE1 + hw_context * 4;
  124. down_write(&pd->driver->sem);
  125. PSB_WSGX32(page_to_pfn(pd->p) << PAGE_SHIFT, offset);
  126. wmb();
  127. psb_mmu_flush_pd_locked(pd->driver, 1);
  128. pd->hw_context = hw_context;
  129. up_write(&pd->driver->sem);
  130. }
  131. static inline unsigned long psb_pd_addr_end(unsigned long addr,
  132. unsigned long end)
  133. {
  134. addr = (addr + PSB_PDE_MASK + 1) & ~PSB_PDE_MASK;
  135. return (addr < end) ? addr : end;
  136. }
  137. static inline uint32_t psb_mmu_mask_pte(uint32_t pfn, int type)
  138. {
  139. uint32_t mask = PSB_PTE_VALID;
  140. if (type & PSB_MMU_CACHED_MEMORY)
  141. mask |= PSB_PTE_CACHED;
  142. if (type & PSB_MMU_RO_MEMORY)
  143. mask |= PSB_PTE_RO;
  144. if (type & PSB_MMU_WO_MEMORY)
  145. mask |= PSB_PTE_WO;
  146. return (pfn << PAGE_SHIFT) | mask;
  147. }
  148. struct psb_mmu_pd *psb_mmu_alloc_pd(struct psb_mmu_driver *driver,
  149. int trap_pagefaults, int invalid_type)
  150. {
  151. struct psb_mmu_pd *pd = kmalloc(sizeof(*pd), GFP_KERNEL);
  152. uint32_t *v;
  153. int i;
  154. if (!pd)
  155. return NULL;
  156. pd->p = alloc_page(GFP_DMA32);
  157. if (!pd->p)
  158. goto out_err1;
  159. pd->dummy_pt = alloc_page(GFP_DMA32);
  160. if (!pd->dummy_pt)
  161. goto out_err2;
  162. pd->dummy_page = alloc_page(GFP_DMA32);
  163. if (!pd->dummy_page)
  164. goto out_err3;
  165. if (!trap_pagefaults) {
  166. pd->invalid_pde = psb_mmu_mask_pte(page_to_pfn(pd->dummy_pt),
  167. invalid_type);
  168. pd->invalid_pte = psb_mmu_mask_pte(page_to_pfn(pd->dummy_page),
  169. invalid_type);
  170. } else {
  171. pd->invalid_pde = 0;
  172. pd->invalid_pte = 0;
  173. }
  174. v = kmap(pd->dummy_pt);
  175. for (i = 0; i < (PAGE_SIZE / sizeof(uint32_t)); ++i)
  176. v[i] = pd->invalid_pte;
  177. kunmap(pd->dummy_pt);
  178. v = kmap(pd->p);
  179. for (i = 0; i < (PAGE_SIZE / sizeof(uint32_t)); ++i)
  180. v[i] = pd->invalid_pde;
  181. kunmap(pd->p);
  182. clear_page(kmap(pd->dummy_page));
  183. kunmap(pd->dummy_page);
  184. pd->tables = vmalloc_user(sizeof(struct psb_mmu_pt *) * 1024);
  185. if (!pd->tables)
  186. goto out_err4;
  187. pd->hw_context = -1;
  188. pd->pd_mask = PSB_PTE_VALID;
  189. pd->driver = driver;
  190. return pd;
  191. out_err4:
  192. __free_page(pd->dummy_page);
  193. out_err3:
  194. __free_page(pd->dummy_pt);
  195. out_err2:
  196. __free_page(pd->p);
  197. out_err1:
  198. kfree(pd);
  199. return NULL;
  200. }
  201. static void psb_mmu_free_pt(struct psb_mmu_pt *pt)
  202. {
  203. __free_page(pt->p);
  204. kfree(pt);
  205. }
  206. void psb_mmu_free_pagedir(struct psb_mmu_pd *pd)
  207. {
  208. struct psb_mmu_driver *driver = pd->driver;
  209. struct drm_device *dev = driver->dev;
  210. struct drm_psb_private *dev_priv = dev->dev_private;
  211. struct psb_mmu_pt *pt;
  212. int i;
  213. down_write(&driver->sem);
  214. if (pd->hw_context != -1) {
  215. PSB_WSGX32(0, PSB_CR_BIF_DIR_LIST_BASE0 + pd->hw_context * 4);
  216. psb_mmu_flush_pd_locked(driver, 1);
  217. }
  218. /* Should take the spinlock here, but we don't need to do that
  219. since we have the semaphore in write mode. */
  220. for (i = 0; i < 1024; ++i) {
  221. pt = pd->tables[i];
  222. if (pt)
  223. psb_mmu_free_pt(pt);
  224. }
  225. vfree(pd->tables);
  226. __free_page(pd->dummy_page);
  227. __free_page(pd->dummy_pt);
  228. __free_page(pd->p);
  229. kfree(pd);
  230. up_write(&driver->sem);
  231. }
  232. static struct psb_mmu_pt *psb_mmu_alloc_pt(struct psb_mmu_pd *pd)
  233. {
  234. struct psb_mmu_pt *pt = kmalloc(sizeof(*pt), GFP_KERNEL);
  235. void *v;
  236. uint32_t clflush_add = pd->driver->clflush_add >> PAGE_SHIFT;
  237. uint32_t clflush_count = PAGE_SIZE / clflush_add;
  238. spinlock_t *lock = &pd->driver->lock;
  239. uint8_t *clf;
  240. uint32_t *ptes;
  241. int i;
  242. if (!pt)
  243. return NULL;
  244. pt->p = alloc_page(GFP_DMA32);
  245. if (!pt->p) {
  246. kfree(pt);
  247. return NULL;
  248. }
  249. spin_lock(lock);
  250. v = kmap_atomic(pt->p);
  251. clf = (uint8_t *) v;
  252. ptes = (uint32_t *) v;
  253. for (i = 0; i < (PAGE_SIZE / sizeof(uint32_t)); ++i)
  254. *ptes++ = pd->invalid_pte;
  255. #if defined(CONFIG_X86)
  256. if (pd->driver->has_clflush && pd->hw_context != -1) {
  257. mb();
  258. for (i = 0; i < clflush_count; ++i) {
  259. psb_clflush(clf);
  260. clf += clflush_add;
  261. }
  262. mb();
  263. }
  264. #endif
  265. kunmap_atomic(v);
  266. spin_unlock(lock);
  267. pt->count = 0;
  268. pt->pd = pd;
  269. pt->index = 0;
  270. return pt;
  271. }
  272. struct psb_mmu_pt *psb_mmu_pt_alloc_map_lock(struct psb_mmu_pd *pd,
  273. unsigned long addr)
  274. {
  275. uint32_t index = psb_mmu_pd_index(addr);
  276. struct psb_mmu_pt *pt;
  277. uint32_t *v;
  278. spinlock_t *lock = &pd->driver->lock;
  279. spin_lock(lock);
  280. pt = pd->tables[index];
  281. while (!pt) {
  282. spin_unlock(lock);
  283. pt = psb_mmu_alloc_pt(pd);
  284. if (!pt)
  285. return NULL;
  286. spin_lock(lock);
  287. if (pd->tables[index]) {
  288. spin_unlock(lock);
  289. psb_mmu_free_pt(pt);
  290. spin_lock(lock);
  291. pt = pd->tables[index];
  292. continue;
  293. }
  294. v = kmap_atomic(pd->p);
  295. pd->tables[index] = pt;
  296. v[index] = (page_to_pfn(pt->p) << 12) | pd->pd_mask;
  297. pt->index = index;
  298. kunmap_atomic((void *) v);
  299. if (pd->hw_context != -1) {
  300. psb_mmu_clflush(pd->driver, (void *)&v[index]);
  301. atomic_set(&pd->driver->needs_tlbflush, 1);
  302. }
  303. }
  304. pt->v = kmap_atomic(pt->p);
  305. return pt;
  306. }
  307. static struct psb_mmu_pt *psb_mmu_pt_map_lock(struct psb_mmu_pd *pd,
  308. unsigned long addr)
  309. {
  310. uint32_t index = psb_mmu_pd_index(addr);
  311. struct psb_mmu_pt *pt;
  312. spinlock_t *lock = &pd->driver->lock;
  313. spin_lock(lock);
  314. pt = pd->tables[index];
  315. if (!pt) {
  316. spin_unlock(lock);
  317. return NULL;
  318. }
  319. pt->v = kmap_atomic(pt->p);
  320. return pt;
  321. }
  322. static void psb_mmu_pt_unmap_unlock(struct psb_mmu_pt *pt)
  323. {
  324. struct psb_mmu_pd *pd = pt->pd;
  325. uint32_t *v;
  326. kunmap_atomic(pt->v);
  327. if (pt->count == 0) {
  328. v = kmap_atomic(pd->p);
  329. v[pt->index] = pd->invalid_pde;
  330. pd->tables[pt->index] = NULL;
  331. if (pd->hw_context != -1) {
  332. psb_mmu_clflush(pd->driver, (void *)&v[pt->index]);
  333. atomic_set(&pd->driver->needs_tlbflush, 1);
  334. }
  335. kunmap_atomic(pt->v);
  336. spin_unlock(&pd->driver->lock);
  337. psb_mmu_free_pt(pt);
  338. return;
  339. }
  340. spin_unlock(&pd->driver->lock);
  341. }
  342. static inline void psb_mmu_set_pte(struct psb_mmu_pt *pt, unsigned long addr,
  343. uint32_t pte)
  344. {
  345. pt->v[psb_mmu_pt_index(addr)] = pte;
  346. }
  347. static inline void psb_mmu_invalidate_pte(struct psb_mmu_pt *pt,
  348. unsigned long addr)
  349. {
  350. pt->v[psb_mmu_pt_index(addr)] = pt->pd->invalid_pte;
  351. }
  352. struct psb_mmu_pd *psb_mmu_get_default_pd(struct psb_mmu_driver *driver)
  353. {
  354. struct psb_mmu_pd *pd;
  355. down_read(&driver->sem);
  356. pd = driver->default_pd;
  357. up_read(&driver->sem);
  358. return pd;
  359. }
  360. /* Returns the physical address of the PD shared by sgx/msvdx */
  361. uint32_t psb_get_default_pd_addr(struct psb_mmu_driver *driver)
  362. {
  363. struct psb_mmu_pd *pd;
  364. pd = psb_mmu_get_default_pd(driver);
  365. return page_to_pfn(pd->p) << PAGE_SHIFT;
  366. }
  367. void psb_mmu_driver_takedown(struct psb_mmu_driver *driver)
  368. {
  369. struct drm_device *dev = driver->dev;
  370. struct drm_psb_private *dev_priv = dev->dev_private;
  371. PSB_WSGX32(driver->bif_ctrl, PSB_CR_BIF_CTRL);
  372. psb_mmu_free_pagedir(driver->default_pd);
  373. kfree(driver);
  374. }
  375. struct psb_mmu_driver *psb_mmu_driver_init(struct drm_device *dev,
  376. int trap_pagefaults,
  377. int invalid_type,
  378. atomic_t *msvdx_mmu_invaldc)
  379. {
  380. struct psb_mmu_driver *driver;
  381. struct drm_psb_private *dev_priv = dev->dev_private;
  382. driver = kmalloc(sizeof(*driver), GFP_KERNEL);
  383. if (!driver)
  384. return NULL;
  385. driver->dev = dev;
  386. driver->default_pd = psb_mmu_alloc_pd(driver, trap_pagefaults,
  387. invalid_type);
  388. if (!driver->default_pd)
  389. goto out_err1;
  390. spin_lock_init(&driver->lock);
  391. init_rwsem(&driver->sem);
  392. down_write(&driver->sem);
  393. atomic_set(&driver->needs_tlbflush, 1);
  394. driver->msvdx_mmu_invaldc = msvdx_mmu_invaldc;
  395. driver->bif_ctrl = PSB_RSGX32(PSB_CR_BIF_CTRL);
  396. PSB_WSGX32(driver->bif_ctrl | _PSB_CB_CTRL_CLEAR_FAULT,
  397. PSB_CR_BIF_CTRL);
  398. PSB_WSGX32(driver->bif_ctrl & ~_PSB_CB_CTRL_CLEAR_FAULT,
  399. PSB_CR_BIF_CTRL);
  400. driver->has_clflush = 0;
  401. #if defined(CONFIG_X86)
  402. if (boot_cpu_has(X86_FEATURE_CLFLUSH)) {
  403. uint32_t tfms, misc, cap0, cap4, clflush_size;
  404. /*
  405. * clflush size is determined at kernel setup for x86_64 but not
  406. * for i386. We have to do it here.
  407. */
  408. cpuid(0x00000001, &tfms, &misc, &cap0, &cap4);
  409. clflush_size = ((misc >> 8) & 0xff) * 8;
  410. driver->has_clflush = 1;
  411. driver->clflush_add =
  412. PAGE_SIZE * clflush_size / sizeof(uint32_t);
  413. driver->clflush_mask = driver->clflush_add - 1;
  414. driver->clflush_mask = ~driver->clflush_mask;
  415. }
  416. #endif
  417. up_write(&driver->sem);
  418. return driver;
  419. out_err1:
  420. kfree(driver);
  421. return NULL;
  422. }
  423. #if defined(CONFIG_X86)
  424. static void psb_mmu_flush_ptes(struct psb_mmu_pd *pd, unsigned long address,
  425. uint32_t num_pages, uint32_t desired_tile_stride,
  426. uint32_t hw_tile_stride)
  427. {
  428. struct psb_mmu_pt *pt;
  429. uint32_t rows = 1;
  430. uint32_t i;
  431. unsigned long addr;
  432. unsigned long end;
  433. unsigned long next;
  434. unsigned long add;
  435. unsigned long row_add;
  436. unsigned long clflush_add = pd->driver->clflush_add;
  437. unsigned long clflush_mask = pd->driver->clflush_mask;
  438. if (!pd->driver->has_clflush)
  439. return;
  440. if (hw_tile_stride)
  441. rows = num_pages / desired_tile_stride;
  442. else
  443. desired_tile_stride = num_pages;
  444. add = desired_tile_stride << PAGE_SHIFT;
  445. row_add = hw_tile_stride << PAGE_SHIFT;
  446. mb();
  447. for (i = 0; i < rows; ++i) {
  448. addr = address;
  449. end = addr + add;
  450. do {
  451. next = psb_pd_addr_end(addr, end);
  452. pt = psb_mmu_pt_map_lock(pd, addr);
  453. if (!pt)
  454. continue;
  455. do {
  456. psb_clflush(&pt->v[psb_mmu_pt_index(addr)]);
  457. } while (addr += clflush_add,
  458. (addr & clflush_mask) < next);
  459. psb_mmu_pt_unmap_unlock(pt);
  460. } while (addr = next, next != end);
  461. address += row_add;
  462. }
  463. mb();
  464. }
  465. #else
  466. static void psb_mmu_flush_ptes(struct psb_mmu_pd *pd, unsigned long address,
  467. uint32_t num_pages, uint32_t desired_tile_stride,
  468. uint32_t hw_tile_stride)
  469. {
  470. drm_ttm_cache_flush();
  471. }
  472. #endif
  473. void psb_mmu_remove_pfn_sequence(struct psb_mmu_pd *pd,
  474. unsigned long address, uint32_t num_pages)
  475. {
  476. struct psb_mmu_pt *pt;
  477. unsigned long addr;
  478. unsigned long end;
  479. unsigned long next;
  480. unsigned long f_address = address;
  481. down_read(&pd->driver->sem);
  482. addr = address;
  483. end = addr + (num_pages << PAGE_SHIFT);
  484. do {
  485. next = psb_pd_addr_end(addr, end);
  486. pt = psb_mmu_pt_alloc_map_lock(pd, addr);
  487. if (!pt)
  488. goto out;
  489. do {
  490. psb_mmu_invalidate_pte(pt, addr);
  491. --pt->count;
  492. } while (addr += PAGE_SIZE, addr < next);
  493. psb_mmu_pt_unmap_unlock(pt);
  494. } while (addr = next, next != end);
  495. out:
  496. if (pd->hw_context != -1)
  497. psb_mmu_flush_ptes(pd, f_address, num_pages, 1, 1);
  498. up_read(&pd->driver->sem);
  499. if (pd->hw_context != -1)
  500. psb_mmu_flush(pd->driver);
  501. return;
  502. }
  503. void psb_mmu_remove_pages(struct psb_mmu_pd *pd, unsigned long address,
  504. uint32_t num_pages, uint32_t desired_tile_stride,
  505. uint32_t hw_tile_stride)
  506. {
  507. struct psb_mmu_pt *pt;
  508. uint32_t rows = 1;
  509. uint32_t i;
  510. unsigned long addr;
  511. unsigned long end;
  512. unsigned long next;
  513. unsigned long add;
  514. unsigned long row_add;
  515. unsigned long f_address = address;
  516. if (hw_tile_stride)
  517. rows = num_pages / desired_tile_stride;
  518. else
  519. desired_tile_stride = num_pages;
  520. add = desired_tile_stride << PAGE_SHIFT;
  521. row_add = hw_tile_stride << PAGE_SHIFT;
  522. down_read(&pd->driver->sem);
  523. /* Make sure we only need to flush this processor's cache */
  524. for (i = 0; i < rows; ++i) {
  525. addr = address;
  526. end = addr + add;
  527. do {
  528. next = psb_pd_addr_end(addr, end);
  529. pt = psb_mmu_pt_map_lock(pd, addr);
  530. if (!pt)
  531. continue;
  532. do {
  533. psb_mmu_invalidate_pte(pt, addr);
  534. --pt->count;
  535. } while (addr += PAGE_SIZE, addr < next);
  536. psb_mmu_pt_unmap_unlock(pt);
  537. } while (addr = next, next != end);
  538. address += row_add;
  539. }
  540. if (pd->hw_context != -1)
  541. psb_mmu_flush_ptes(pd, f_address, num_pages,
  542. desired_tile_stride, hw_tile_stride);
  543. up_read(&pd->driver->sem);
  544. if (pd->hw_context != -1)
  545. psb_mmu_flush(pd->driver);
  546. }
  547. int psb_mmu_insert_pfn_sequence(struct psb_mmu_pd *pd, uint32_t start_pfn,
  548. unsigned long address, uint32_t num_pages,
  549. int type)
  550. {
  551. struct psb_mmu_pt *pt;
  552. uint32_t pte;
  553. unsigned long addr;
  554. unsigned long end;
  555. unsigned long next;
  556. unsigned long f_address = address;
  557. int ret = -ENOMEM;
  558. down_read(&pd->driver->sem);
  559. addr = address;
  560. end = addr + (num_pages << PAGE_SHIFT);
  561. do {
  562. next = psb_pd_addr_end(addr, end);
  563. pt = psb_mmu_pt_alloc_map_lock(pd, addr);
  564. if (!pt) {
  565. ret = -ENOMEM;
  566. goto out;
  567. }
  568. do {
  569. pte = psb_mmu_mask_pte(start_pfn++, type);
  570. psb_mmu_set_pte(pt, addr, pte);
  571. pt->count++;
  572. } while (addr += PAGE_SIZE, addr < next);
  573. psb_mmu_pt_unmap_unlock(pt);
  574. } while (addr = next, next != end);
  575. ret = 0;
  576. out:
  577. if (pd->hw_context != -1)
  578. psb_mmu_flush_ptes(pd, f_address, num_pages, 1, 1);
  579. up_read(&pd->driver->sem);
  580. if (pd->hw_context != -1)
  581. psb_mmu_flush(pd->driver);
  582. return 0;
  583. }
  584. int psb_mmu_insert_pages(struct psb_mmu_pd *pd, struct page **pages,
  585. unsigned long address, uint32_t num_pages,
  586. uint32_t desired_tile_stride, uint32_t hw_tile_stride,
  587. int type)
  588. {
  589. struct psb_mmu_pt *pt;
  590. uint32_t rows = 1;
  591. uint32_t i;
  592. uint32_t pte;
  593. unsigned long addr;
  594. unsigned long end;
  595. unsigned long next;
  596. unsigned long add;
  597. unsigned long row_add;
  598. unsigned long f_address = address;
  599. int ret = -ENOMEM;
  600. if (hw_tile_stride) {
  601. if (num_pages % desired_tile_stride != 0)
  602. return -EINVAL;
  603. rows = num_pages / desired_tile_stride;
  604. } else {
  605. desired_tile_stride = num_pages;
  606. }
  607. add = desired_tile_stride << PAGE_SHIFT;
  608. row_add = hw_tile_stride << PAGE_SHIFT;
  609. down_read(&pd->driver->sem);
  610. for (i = 0; i < rows; ++i) {
  611. addr = address;
  612. end = addr + add;
  613. do {
  614. next = psb_pd_addr_end(addr, end);
  615. pt = psb_mmu_pt_alloc_map_lock(pd, addr);
  616. if (!pt)
  617. goto out;
  618. do {
  619. pte = psb_mmu_mask_pte(page_to_pfn(*pages++),
  620. type);
  621. psb_mmu_set_pte(pt, addr, pte);
  622. pt->count++;
  623. } while (addr += PAGE_SIZE, addr < next);
  624. psb_mmu_pt_unmap_unlock(pt);
  625. } while (addr = next, next != end);
  626. address += row_add;
  627. }
  628. ret = 0;
  629. out:
  630. if (pd->hw_context != -1)
  631. psb_mmu_flush_ptes(pd, f_address, num_pages,
  632. desired_tile_stride, hw_tile_stride);
  633. up_read(&pd->driver->sem);
  634. if (pd->hw_context != -1)
  635. psb_mmu_flush(pd->driver);
  636. return ret;
  637. }
  638. int psb_mmu_virtual_to_pfn(struct psb_mmu_pd *pd, uint32_t virtual,
  639. unsigned long *pfn)
  640. {
  641. int ret;
  642. struct psb_mmu_pt *pt;
  643. uint32_t tmp;
  644. spinlock_t *lock = &pd->driver->lock;
  645. down_read(&pd->driver->sem);
  646. pt = psb_mmu_pt_map_lock(pd, virtual);
  647. if (!pt) {
  648. uint32_t *v;
  649. spin_lock(lock);
  650. v = kmap_atomic(pd->p);
  651. tmp = v[psb_mmu_pd_index(virtual)];
  652. kunmap_atomic(v);
  653. spin_unlock(lock);
  654. if (tmp != pd->invalid_pde || !(tmp & PSB_PTE_VALID) ||
  655. !(pd->invalid_pte & PSB_PTE_VALID)) {
  656. ret = -EINVAL;
  657. goto out;
  658. }
  659. ret = 0;
  660. *pfn = pd->invalid_pte >> PAGE_SHIFT;
  661. goto out;
  662. }
  663. tmp = pt->v[psb_mmu_pt_index(virtual)];
  664. if (!(tmp & PSB_PTE_VALID)) {
  665. ret = -EINVAL;
  666. } else {
  667. ret = 0;
  668. *pfn = tmp >> PAGE_SHIFT;
  669. }
  670. psb_mmu_pt_unmap_unlock(pt);
  671. out:
  672. up_read(&pd->driver->sem);
  673. return ret;
  674. }