sq.c 12 KB

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