sq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * arch/sh/kernel/cpu/sq.c
  3. *
  4. * General management API for SH-4 integrated Store Queues
  5. *
  6. * Copyright (C) 2001, 2002, 2003, 2004 Paul Mundt
  7. * Copyright (C) 2001, 2002 M. R. Brown
  8. *
  9. * Some of this code has been adopted directly from the old arch/sh/mm/sq.c
  10. * hack that was part of the LinuxDC project. For all intents and purposes,
  11. * this is a completely new interface that really doesn't have much in common
  12. * with the old zone-based approach at all. In fact, it's only listed here for
  13. * general completeness.
  14. *
  15. * This file is subject to the terms and conditions of the GNU General Public
  16. * License. See the file "COPYING" in the main directory of this archive
  17. * for more details.
  18. */
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/list.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/mm.h>
  28. #include <asm/io.h>
  29. #include <asm/page.h>
  30. #include <asm/cacheflush.h>
  31. #include <asm/mmu_context.h>
  32. #include <asm/cpu/sq.h>
  33. static LIST_HEAD(sq_mapping_list);
  34. static DEFINE_SPINLOCK(sq_mapping_lock);
  35. /**
  36. * sq_flush - Flush (prefetch) the store queue cache
  37. * @addr: the store queue address to flush
  38. *
  39. * Executes a prefetch instruction on the specified store queue cache,
  40. * so that the cached data is written to physical memory.
  41. */
  42. inline void sq_flush(void *addr)
  43. {
  44. __asm__ __volatile__ ("pref @%0" : : "r" (addr) : "memory");
  45. }
  46. /**
  47. * sq_flush_range - Flush (prefetch) a specific SQ range
  48. * @start: the store queue address to start flushing from
  49. * @len: the length to flush
  50. *
  51. * Flushes the store queue cache from @start to @start + @len in a
  52. * linear fashion.
  53. */
  54. void sq_flush_range(unsigned long start, unsigned int len)
  55. {
  56. volatile unsigned long *sq = (unsigned long *)start;
  57. unsigned long dummy;
  58. /* Flush the queues */
  59. for (len >>= 5; len--; sq += 8)
  60. sq_flush((void *)sq);
  61. /* Wait for completion */
  62. dummy = ctrl_inl(P4SEG_STORE_QUE);
  63. ctrl_outl(0, P4SEG_STORE_QUE + 0);
  64. ctrl_outl(0, P4SEG_STORE_QUE + 8);
  65. }
  66. static struct sq_mapping *__sq_alloc_mapping(unsigned long virt, unsigned long phys, unsigned long size, const char *name)
  67. {
  68. struct sq_mapping *map;
  69. if (virt + size > SQ_ADDRMAX)
  70. return ERR_PTR(-ENOSPC);
  71. map = kmalloc(sizeof(struct sq_mapping), GFP_KERNEL);
  72. if (!map)
  73. return ERR_PTR(-ENOMEM);
  74. INIT_LIST_HEAD(&map->list);
  75. map->sq_addr = virt;
  76. map->addr = phys;
  77. map->size = size + 1;
  78. map->name = name;
  79. list_add(&map->list, &sq_mapping_list);
  80. return map;
  81. }
  82. static unsigned long __sq_get_next_addr(void)
  83. {
  84. if (!list_empty(&sq_mapping_list)) {
  85. struct list_head *pos, *tmp;
  86. /*
  87. * Read one off the list head, as it will have the highest
  88. * mapped allocation. Set the next one up right above it.
  89. *
  90. * This is somewhat sub-optimal, as we don't look at
  91. * gaps between allocations or anything lower then the
  92. * highest-level allocation.
  93. *
  94. * However, in the interest of performance and the general
  95. * lack of desire to do constant list rebalancing, we don't
  96. * worry about it.
  97. */
  98. list_for_each_safe(pos, tmp, &sq_mapping_list) {
  99. struct sq_mapping *entry;
  100. entry = list_entry(pos, typeof(*entry), list);
  101. return entry->sq_addr + entry->size;
  102. }
  103. }
  104. return P4SEG_STORE_QUE;
  105. }
  106. /**
  107. * __sq_remap - Perform a translation from the SQ to a phys addr
  108. * @map: sq mapping containing phys and store queue addresses.
  109. *
  110. * Maps the store queue address specified in the mapping to the physical
  111. * address specified in the mapping.
  112. */
  113. static struct sq_mapping *__sq_remap(struct sq_mapping *map)
  114. {
  115. unsigned long flags, pteh, ptel;
  116. struct vm_struct *vma;
  117. pgprot_t pgprot;
  118. /*
  119. * Without an MMU (or with it turned off), this is much more
  120. * straightforward, as we can just load up each queue's QACR with
  121. * the physical address appropriately masked.
  122. */
  123. ctrl_outl(((map->addr >> 26) << 2) & 0x1c, SQ_QACR0);
  124. ctrl_outl(((map->addr >> 26) << 2) & 0x1c, SQ_QACR1);
  125. #ifdef CONFIG_MMU
  126. /*
  127. * With an MMU on the other hand, things are slightly more involved.
  128. * Namely, we have to have a direct mapping between the SQ addr and
  129. * the associated physical address in the UTLB by way of setting up
  130. * a virt<->phys translation by hand. We do this by simply specifying
  131. * the SQ addr in UTLB.VPN and the associated physical address in
  132. * UTLB.PPN.
  133. *
  134. * Notably, even though this is a special case translation, and some
  135. * of the configuration bits are meaningless, we're still required
  136. * to have a valid ASID context in PTEH.
  137. *
  138. * We could also probably get by without explicitly setting PTEA, but
  139. * we do it here just for good measure.
  140. */
  141. spin_lock_irqsave(&sq_mapping_lock, flags);
  142. pteh = map->sq_addr;
  143. ctrl_outl((pteh & MMU_VPN_MASK) | get_asid(), MMU_PTEH);
  144. ptel = map->addr & PAGE_MASK;
  145. #ifndef CONFIG_CPU_SUBTYPE_SH7780
  146. ctrl_outl(((ptel >> 28) & 0xe) | (ptel & 0x1), MMU_PTEA);
  147. #endif
  148. pgprot = pgprot_noncached(PAGE_KERNEL);
  149. ptel &= _PAGE_FLAGS_HARDWARE_MASK;
  150. ptel |= pgprot_val(pgprot);
  151. ctrl_outl(ptel, MMU_PTEL);
  152. __asm__ __volatile__ ("ldtlb" : : : "memory");
  153. spin_unlock_irqrestore(&sq_mapping_lock, flags);
  154. /*
  155. * Next, we need to map ourselves in the kernel page table, so that
  156. * future accesses after a TLB flush will be handled when we take a
  157. * page fault.
  158. *
  159. * Theoretically we could just do this directly and not worry about
  160. * setting up the translation by hand ahead of time, but for the
  161. * cases where we want a one-shot SQ mapping followed by a quick
  162. * writeout before we hit the TLB flush, we do it anyways. This way
  163. * we at least save ourselves the initial page fault overhead.
  164. */
  165. vma = __get_vm_area(map->size, VM_ALLOC, map->sq_addr, SQ_ADDRMAX);
  166. if (!vma)
  167. return ERR_PTR(-ENOMEM);
  168. vma->phys_addr = map->addr;
  169. if (remap_area_pages((unsigned long)vma->addr, vma->phys_addr,
  170. map->size, pgprot_val(pgprot))) {
  171. vunmap(vma->addr);
  172. return NULL;
  173. }
  174. #endif /* CONFIG_MMU */
  175. return map;
  176. }
  177. /**
  178. * sq_remap - Map a physical address through the Store Queues
  179. * @phys: Physical address of mapping.
  180. * @size: Length of mapping.
  181. * @name: User invoking mapping.
  182. *
  183. * Remaps the physical address @phys through the next available store queue
  184. * address of @size length. @name is logged at boot time as well as through
  185. * the procfs interface.
  186. *
  187. * A pre-allocated and filled sq_mapping pointer is returned, and must be
  188. * cleaned up with a call to sq_unmap() when the user is done with the
  189. * mapping.
  190. */
  191. struct sq_mapping *sq_remap(unsigned long phys, unsigned int size, const char *name)
  192. {
  193. struct sq_mapping *map;
  194. unsigned long virt, end;
  195. unsigned int psz;
  196. /* Don't allow wraparound or zero size */
  197. end = phys + size - 1;
  198. if (!size || end < phys)
  199. return NULL;
  200. /* Don't allow anyone to remap normal memory.. */
  201. if (phys < virt_to_phys(high_memory))
  202. return NULL;
  203. phys &= PAGE_MASK;
  204. size = PAGE_ALIGN(end + 1) - phys;
  205. virt = __sq_get_next_addr();
  206. psz = (size + (PAGE_SIZE - 1)) / PAGE_SIZE;
  207. map = __sq_alloc_mapping(virt, phys, size, name);
  208. printk("sqremap: %15s [%4d page%s] va 0x%08lx pa 0x%08lx\n",
  209. map->name ? map->name : "???",
  210. psz, psz == 1 ? " " : "s",
  211. map->sq_addr, map->addr);
  212. return __sq_remap(map);
  213. }
  214. /**
  215. * sq_unmap - Unmap a Store Queue allocation
  216. * @map: Pre-allocated Store Queue mapping.
  217. *
  218. * Unmaps the store queue allocation @map that was previously created by
  219. * sq_remap(). Also frees up the pte that was previously inserted into
  220. * the kernel page table and discards the UTLB translation.
  221. */
  222. void sq_unmap(struct sq_mapping *map)
  223. {
  224. if (map->sq_addr > (unsigned long)high_memory)
  225. vfree((void *)(map->sq_addr & PAGE_MASK));
  226. list_del(&map->list);
  227. kfree(map);
  228. }
  229. /**
  230. * sq_clear - Clear a store queue range
  231. * @addr: Address to start clearing from.
  232. * @len: Length to clear.
  233. *
  234. * A quick zero-fill implementation for clearing out memory that has been
  235. * remapped through the store queues.
  236. */
  237. void sq_clear(unsigned long addr, unsigned int len)
  238. {
  239. int i;
  240. /* Clear out both queues linearly */
  241. for (i = 0; i < 8; i++) {
  242. ctrl_outl(0, addr + i + 0);
  243. ctrl_outl(0, addr + i + 8);
  244. }
  245. sq_flush_range(addr, len);
  246. }
  247. /**
  248. * sq_vma_unmap - Unmap a VMA range
  249. * @area: VMA containing range.
  250. * @addr: Start of range.
  251. * @len: Length of range.
  252. *
  253. * Searches the sq_mapping_list for a mapping matching the sq addr @addr,
  254. * and subsequently frees up the entry. Further cleanup is done by generic
  255. * code.
  256. */
  257. static void sq_vma_unmap(struct vm_area_struct *area,
  258. unsigned long addr, size_t len)
  259. {
  260. struct list_head *pos, *tmp;
  261. list_for_each_safe(pos, tmp, &sq_mapping_list) {
  262. struct sq_mapping *entry;
  263. entry = list_entry(pos, typeof(*entry), list);
  264. if (entry->sq_addr == addr) {
  265. /*
  266. * We could probably get away without doing the tlb flush
  267. * here, as generic code should take care of most of this
  268. * when unmapping the rest of the VMA range for us. Leave
  269. * it in for added sanity for the time being..
  270. */
  271. __flush_tlb_page(get_asid(), entry->sq_addr & PAGE_MASK);
  272. list_del(&entry->list);
  273. kfree(entry);
  274. return;
  275. }
  276. }
  277. }
  278. /**
  279. * sq_vma_sync - Sync a VMA range
  280. * @area: VMA containing range.
  281. * @start: Start of range.
  282. * @len: Length of range.
  283. * @flags: Additional flags.
  284. *
  285. * Synchronizes an sq mapped range by flushing the store queue cache for
  286. * the duration of the mapping.
  287. *
  288. * Used internally for user mappings, which must use msync() to prefetch
  289. * the store queue cache.
  290. */
  291. static int sq_vma_sync(struct vm_area_struct *area,
  292. unsigned long start, size_t len, unsigned int flags)
  293. {
  294. sq_flush_range(start, len);
  295. return 0;
  296. }
  297. static struct vm_operations_struct sq_vma_ops = {
  298. .unmap = sq_vma_unmap,
  299. .sync = sq_vma_sync,
  300. };
  301. /**
  302. * sq_mmap - mmap() for /dev/cpu/sq
  303. * @file: unused.
  304. * @vma: VMA to remap.
  305. *
  306. * Remap the specified vma @vma through the store queues, and setup associated
  307. * information for the new mapping. Also build up the page tables for the new
  308. * area.
  309. */
  310. static int sq_mmap(struct file *file, struct vm_area_struct *vma)
  311. {
  312. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  313. unsigned long size = vma->vm_end - vma->vm_start;
  314. struct sq_mapping *map;
  315. /*
  316. * We're not interested in any arbitrary virtual address that has
  317. * been stuck in the VMA, as we already know what addresses we
  318. * want. Save off the size, and reposition the VMA to begin at
  319. * the next available sq address.
  320. */
  321. vma->vm_start = __sq_get_next_addr();
  322. vma->vm_end = vma->vm_start + size;
  323. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  324. vma->vm_flags |= VM_IO | VM_RESERVED;
  325. map = __sq_alloc_mapping(vma->vm_start, offset, size, "Userspace");
  326. if (io_remap_pfn_range(vma, map->sq_addr, map->addr >> PAGE_SHIFT,
  327. size, vma->vm_page_prot))
  328. return -EAGAIN;
  329. vma->vm_ops = &sq_vma_ops;
  330. return 0;
  331. }
  332. #ifdef CONFIG_PROC_FS
  333. static int sq_mapping_read_proc(char *buf, char **start, off_t off,
  334. int len, int *eof, void *data)
  335. {
  336. struct list_head *pos;
  337. char *p = buf;
  338. list_for_each_prev(pos, &sq_mapping_list) {
  339. struct sq_mapping *entry;
  340. entry = list_entry(pos, typeof(*entry), list);
  341. p += sprintf(p, "%08lx-%08lx [%08lx]: %s\n", entry->sq_addr,
  342. entry->sq_addr + entry->size - 1, entry->addr,
  343. entry->name);
  344. }
  345. return p - buf;
  346. }
  347. #endif
  348. static struct file_operations sq_fops = {
  349. .owner = THIS_MODULE,
  350. .mmap = sq_mmap,
  351. };
  352. static struct miscdevice sq_dev = {
  353. .minor = STORE_QUEUE_MINOR,
  354. .name = "sq",
  355. .fops = &sq_fops,
  356. };
  357. static int __init sq_api_init(void)
  358. {
  359. int ret;
  360. printk(KERN_NOTICE "sq: Registering store queue API.\n");
  361. create_proc_read_entry("sq_mapping", 0, 0, sq_mapping_read_proc, 0);
  362. ret = misc_register(&sq_dev);
  363. if (ret)
  364. remove_proc_entry("sq_mapping", NULL);
  365. return ret;
  366. }
  367. static void __exit sq_api_exit(void)
  368. {
  369. misc_deregister(&sq_dev);
  370. remove_proc_entry("sq_mapping", NULL);
  371. }
  372. module_init(sq_api_init);
  373. module_exit(sq_api_exit);
  374. MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, M. R. Brown <mrbrown@0xd6.org>");
  375. MODULE_DESCRIPTION("Simple API for SH-4 integrated Store Queues");
  376. MODULE_LICENSE("GPL");
  377. MODULE_ALIAS_MISCDEV(STORE_QUEUE_MINOR);
  378. EXPORT_SYMBOL(sq_remap);
  379. EXPORT_SYMBOL(sq_unmap);
  380. EXPORT_SYMBOL(sq_clear);
  381. EXPORT_SYMBOL(sq_flush);
  382. EXPORT_SYMBOL(sq_flush_range);