x86: fix Xorg startup/shutdown slowdown with PAT
[safe/jmp/linux-2.6] / arch / x86 / mm / pat.c
index 49dcd96..bb6e8a2 100644 (file)
@@ -12,6 +12,8 @@
 #include <linux/gfp.h>
 #include <linux/fs.h>
 #include <linux/bootmem.h>
+#include <linux/debugfs.h>
+#include <linux/seq_file.h>
 
 #include <asm/msr.h>
 #include <asm/tlbflush.h>
@@ -178,6 +180,36 @@ static unsigned long pat_x_mtrr_type(u64 start, u64 end, unsigned long req_type)
        return req_type;
 }
 
+static int chk_conflict(struct memtype *new, struct memtype *entry,
+                       unsigned long *type)
+{
+       if (new->type != entry->type) {
+               if (type) {
+                       new->type = entry->type;
+                       *type = entry->type;
+               } else
+                       goto conflict;
+       }
+
+        /* check overlaps with more than one entry in the list */
+       list_for_each_entry_continue(entry, &memtype_list, nd) {
+               if (new->end <= entry->start)
+                       break;
+               else if (new->type != entry->type)
+                       goto conflict;
+       }
+       return 0;
+
+ conflict:
+       printk(KERN_INFO "%s:%d conflicting memory types "
+              "%Lx-%Lx %s<->%s\n", current->comm, current->pid, new->start,
+              new->end, cattr_name(new->type), cattr_name(entry->type));
+       return -EBUSY;
+}
+
+static struct memtype *cached_entry;
+static u64 cached_start;
+
 /*
  * req_type typically has one of the:
  * - _PAGE_CACHE_WB
@@ -251,97 +283,53 @@ int reserve_memtype(u64 start, u64 end, unsigned long req_type,
 
        spin_lock(&memtype_lock);
 
+       if (cached_entry && start >= cached_start)
+               entry = cached_entry;
+       else
+               entry = list_entry(&memtype_list, struct memtype, nd);
+
        /* Search for existing mapping that overlaps the current range */
        where = NULL;
-       list_for_each_entry(entry, &memtype_list, nd) {
-               struct memtype *saved_ptr;
-
-               if (entry->start >= end) {
+       list_for_each_entry_continue(entry, &memtype_list, nd) {
+               if (end <= entry->start) {
                        where = entry->nd.prev;
+                       cached_entry = list_entry(where, struct memtype, nd);
                        break;
-               }
-
-               if (start <= entry->start && end >= entry->start) {
-                       if (actual_type != entry->type && new_type) {
-                               actual_type = entry->type;
-                               *new_type = actual_type;
-                               new->type = actual_type;
-                       }
-
-                       if (actual_type != entry->type) {
-                               err = -EBUSY;
-                               break;
+               } else if (start <= entry->start) { /* end > entry->start */
+                       err = chk_conflict(new, entry, new_type);
+                       if (!err) {
+                               dprintk("Overlap at 0x%Lx-0x%Lx\n",
+                                       entry->start, entry->end);
+                               where = entry->nd.prev;
+                               cached_entry = list_entry(where,
+                                                       struct memtype, nd);
                        }
-
-                       saved_ptr = entry;
-                       /*
-                        * Check to see whether the request overlaps more
-                        * than one entry in the list
-                        */
-                       list_for_each_entry_continue(entry, &memtype_list, nd) {
-                               if (end <= entry->start) {
-                                       break;
-                               }
-
-                               if (actual_type != entry->type) {
-                                       err = -EBUSY;
-                                       break;
-                               }
-                       }
-
-                       if (err) {
-                               break;
-                       }
-
-                       dprintk("Overlap at 0x%Lx-0x%Lx\n",
-                              saved_ptr->start, saved_ptr->end);
-                       where = saved_ptr->nd.prev;
                        break;
-               }
-
-               if (start < entry->end) {
-                       if (actual_type != entry->type && new_type) {
-                               actual_type = entry->type;
-                               *new_type = actual_type;
-                               new->type = actual_type;
-                       }
-
-                       if (actual_type != entry->type) {
-                               err = -EBUSY;
-                               break;
-                       }
-
-                       saved_ptr = entry;
-                       /*
-                        * Check to see whether the request overlaps more
-                        * than one entry in the list
-                        */
-                       list_for_each_entry_continue(entry, &memtype_list, nd) {
-                               if (end <= entry->start) {
-                                       break;
-                               }
-
-                               if (actual_type != entry->type) {
-                                       err = -EBUSY;
-                                       break;
+               } else if (start < entry->end) { /* start > entry->start */
+                       err = chk_conflict(new, entry, new_type);
+                       if (!err) {
+                               dprintk("Overlap at 0x%Lx-0x%Lx\n",
+                                       entry->start, entry->end);
+                               cached_entry = list_entry(entry->nd.prev,
+                                                       struct memtype, nd);
+
+                               /*
+                                * Move to right position in the linked
+                                * list to add this new entry
+                                */
+                               list_for_each_entry_continue(entry,
+                                                       &memtype_list, nd) {
+                                       if (start <= entry->start) {
+                                               where = entry->nd.prev;
+                                               break;
+                                       }
                                }
                        }
-
-                       if (err) {
-                               break;
-                       }
-
-                       dprintk("Overlap at 0x%Lx-0x%Lx\n",
-                                saved_ptr->start, saved_ptr->end);
-                       where = &saved_ptr->nd;
                        break;
                }
        }
 
        if (err) {
-               printk(KERN_INFO "%s:%d conflicting memory types "
-                      "%Lx-%Lx %s<->%s\n", current->comm, current->pid, start,
-                      end, cattr_name(new->type), cattr_name(entry->type));
                printk(KERN_INFO "reserve_memtype failed 0x%Lx-0x%Lx, "
                       "track %s, req %s\n",
                       start, end, cattr_name(new->type), cattr_name(req_type));
@@ -350,6 +338,8 @@ int reserve_memtype(u64 start, u64 end, unsigned long req_type,
                return err;
        }
 
+       cached_start = start;
+
        if (where)
                list_add(&new->nd, where);
        else
@@ -379,6 +369,9 @@ int free_memtype(u64 start, u64 end)
        spin_lock(&memtype_lock);
        list_for_each_entry(entry, &memtype_list, nd) {
                if (entry->start == start && entry->end == end) {
+                       if (cached_entry == entry || cached_start == start)
+                               cached_entry = NULL;
+
                        list_del(&entry->nd);
                        kfree(entry);
                        err = 0;
@@ -411,8 +404,8 @@ pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
        return vma_prot;
 }
 
-#ifdef CONFIG_NONPROMISC_DEVMEM
-/* This check is done in drivers/char/mem.c in case of NONPROMISC_DEVMEM*/
+#ifdef CONFIG_STRICT_DEVMEM
+/* This check is done in drivers/char/mem.c in case of STRICT_DEVMEM*/
 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
 {
        return 1;
@@ -436,7 +429,7 @@ static inline int range_is_allowed(unsigned long pfn, unsigned long size)
        }
        return 1;
 }
-#endif /* CONFIG_NONPROMISC_DEVMEM */
+#endif /* CONFIG_STRICT_DEVMEM */
 
 int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
                                unsigned long size, pgprot_t *vma_prot)
@@ -487,7 +480,8 @@ int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
        if (retval < 0)
                return 0;
 
-       if (pfn <= max_pfn_mapped &&
+       if (((pfn < max_low_pfn_mapped) ||
+            (pfn >= (1UL<<(32 - PAGE_SHIFT)) && pfn < max_pfn_mapped)) &&
            ioremap_change_attr((unsigned long)__va(offset), size, flags) < 0) {
                free_memtype(offset, offset + size);
                printk(KERN_INFO
@@ -526,3 +520,89 @@ void unmap_devmem(unsigned long pfn, unsigned long size, pgprot_t vma_prot)
 
        free_memtype(addr, addr + size);
 }
+
+#if defined(CONFIG_DEBUG_FS)
+
+/* get Nth element of the linked list */
+static struct memtype *memtype_get_idx(loff_t pos)
+{
+       struct memtype *list_node, *print_entry;
+       int i = 1;
+
+       print_entry  = kmalloc(sizeof(struct memtype), GFP_KERNEL);
+       if (!print_entry)
+               return NULL;
+
+       spin_lock(&memtype_lock);
+       list_for_each_entry(list_node, &memtype_list, nd) {
+               if (pos == i) {
+                       *print_entry = *list_node;
+                       spin_unlock(&memtype_lock);
+                       return print_entry;
+               }
+               ++i;
+       }
+       spin_unlock(&memtype_lock);
+       kfree(print_entry);
+       return NULL;
+}
+
+static void *memtype_seq_start(struct seq_file *seq, loff_t *pos)
+{
+       if (*pos == 0) {
+               ++*pos;
+               seq_printf(seq, "PAT memtype list:\n");
+       }
+
+       return memtype_get_idx(*pos);
+}
+
+static void *memtype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
+{
+       ++*pos;
+       return memtype_get_idx(*pos);
+}
+
+static void memtype_seq_stop(struct seq_file *seq, void *v)
+{
+}
+
+static int memtype_seq_show(struct seq_file *seq, void *v)
+{
+       struct memtype *print_entry = (struct memtype *)v;
+
+       seq_printf(seq, "%s @ 0x%Lx-0x%Lx\n", cattr_name(print_entry->type),
+                       print_entry->start, print_entry->end);
+       kfree(print_entry);
+       return 0;
+}
+
+static struct seq_operations memtype_seq_ops = {
+       .start = memtype_seq_start,
+       .next  = memtype_seq_next,
+       .stop  = memtype_seq_stop,
+       .show  = memtype_seq_show,
+};
+
+static int memtype_seq_open(struct inode *inode, struct file *file)
+{
+       return seq_open(file, &memtype_seq_ops);
+}
+
+static const struct file_operations memtype_fops = {
+       .open    = memtype_seq_open,
+       .read    = seq_read,
+       .llseek  = seq_lseek,
+       .release = seq_release,
+};
+
+static int __init pat_memtype_list_init(void)
+{
+       debugfs_create_file("pat_memtype_list", S_IRUSR, arch_debugfs_dir,
+                               NULL, &memtype_fops);
+       return 0;
+}
+
+late_initcall(pat_memtype_list_init);
+
+#endif /* CONFIG_DEBUG_FS */