1818#include <asm/processor.h>
1919#include <linux/smp.h>
2020#include <asm/k8.h>
21+ #include <asm/smp.h>
2122
2223#define LVL_1_INST 1
2324#define LVL_1_DATA 2
@@ -152,7 +153,8 @@ struct _cpuid4_info {
152153 union _cpuid4_leaf_ebx ebx ;
153154 union _cpuid4_leaf_ecx ecx ;
154155 unsigned long size ;
155- unsigned long can_disable ;
156+ bool can_disable ;
157+ unsigned int l3_indices ;
156158 DECLARE_BITMAP (shared_cpu_map , NR_CPUS );
157159};
158160
@@ -162,7 +164,8 @@ struct _cpuid4_info_regs {
162164 union _cpuid4_leaf_ebx ebx ;
163165 union _cpuid4_leaf_ecx ecx ;
164166 unsigned long size ;
165- unsigned long can_disable ;
167+ bool can_disable ;
168+ unsigned int l3_indices ;
166169};
167170
168171unsigned short num_cache_leaves ;
@@ -292,6 +295,36 @@ amd_cpuid4(int leaf, union _cpuid4_leaf_eax *eax,
292295 (ebx -> split .ways_of_associativity + 1 ) - 1 ;
293296}
294297
298+ struct _cache_attr {
299+ struct attribute attr ;
300+ ssize_t (* show )(struct _cpuid4_info * , char * );
301+ ssize_t (* store )(struct _cpuid4_info * , const char * , size_t count );
302+ };
303+
304+ #ifdef CONFIG_CPU_SUP_AMD
305+ static unsigned int __cpuinit amd_calc_l3_indices (void )
306+ {
307+ /*
308+ * We're called over smp_call_function_single() and therefore
309+ * are on the correct cpu.
310+ */
311+ int cpu = smp_processor_id ();
312+ int node = cpu_to_node (cpu );
313+ struct pci_dev * dev = node_to_k8_nb_misc (node );
314+ unsigned int sc0 , sc1 , sc2 , sc3 ;
315+ u32 val = 0 ;
316+
317+ pci_read_config_dword (dev , 0x1C4 , & val );
318+
319+ /* calculate subcache sizes */
320+ sc0 = !(val & BIT (0 ));
321+ sc1 = !(val & BIT (4 ));
322+ sc2 = !(val & BIT (8 )) + !(val & BIT (9 ));
323+ sc3 = !(val & BIT (12 )) + !(val & BIT (13 ));
324+
325+ return (max (max (max (sc0 , sc1 ), sc2 ), sc3 ) << 10 ) - 1 ;
326+ }
327+
295328static void __cpuinit
296329amd_check_l3_disable (int index , struct _cpuid4_info_regs * this_leaf )
297330{
@@ -301,12 +334,103 @@ amd_check_l3_disable(int index, struct _cpuid4_info_regs *this_leaf)
301334 if (boot_cpu_data .x86 == 0x11 )
302335 return ;
303336
304- /* see erratum #382 */
305- if ((boot_cpu_data .x86 == 0x10 ) && (boot_cpu_data .x86_model < 0x8 ))
337+ /* see errata #382 and #388 */
338+ if ((boot_cpu_data .x86 == 0x10 ) &&
339+ ((boot_cpu_data .x86_model < 0x8 ) ||
340+ (boot_cpu_data .x86_mask < 0x1 )))
306341 return ;
307342
308- this_leaf -> can_disable = 1 ;
343+ this_leaf -> can_disable = true;
344+ this_leaf -> l3_indices = amd_calc_l3_indices ();
345+ }
346+
347+ static ssize_t show_cache_disable (struct _cpuid4_info * this_leaf , char * buf ,
348+ unsigned int index )
349+ {
350+ int cpu = cpumask_first (to_cpumask (this_leaf -> shared_cpu_map ));
351+ int node = amd_get_nb_id (cpu );
352+ struct pci_dev * dev = node_to_k8_nb_misc (node );
353+ unsigned int reg = 0 ;
354+
355+ if (!this_leaf -> can_disable )
356+ return - EINVAL ;
357+
358+ if (!dev )
359+ return - EINVAL ;
360+
361+ pci_read_config_dword (dev , 0x1BC + index * 4 , & reg );
362+ return sprintf (buf , "0x%08x\n" , reg );
363+ }
364+
365+ #define SHOW_CACHE_DISABLE (index ) \
366+ static ssize_t \
367+ show_cache_disable_##index(struct _cpuid4_info *this_leaf, char *buf) \
368+ { \
369+ return show_cache_disable(this_leaf, buf, index); \
370+ }
371+ SHOW_CACHE_DISABLE (0 )
372+ SHOW_CACHE_DISABLE (1 )
373+
374+ static ssize_t store_cache_disable (struct _cpuid4_info * this_leaf ,
375+ const char * buf , size_t count , unsigned int index )
376+ {
377+ int cpu = cpumask_first (to_cpumask (this_leaf -> shared_cpu_map ));
378+ int node = amd_get_nb_id (cpu );
379+ struct pci_dev * dev = node_to_k8_nb_misc (node );
380+ unsigned long val = 0 ;
381+
382+ #define SUBCACHE_MASK (3UL << 20)
383+ #define SUBCACHE_INDEX 0xfff
384+
385+ if (!this_leaf -> can_disable )
386+ return - EINVAL ;
387+
388+ if (!capable (CAP_SYS_ADMIN ))
389+ return - EPERM ;
390+
391+ if (!dev )
392+ return - EINVAL ;
393+
394+ if (strict_strtoul (buf , 10 , & val ) < 0 )
395+ return - EINVAL ;
396+
397+ /* do not allow writes outside of allowed bits */
398+ if ((val & ~(SUBCACHE_MASK | SUBCACHE_INDEX )) ||
399+ ((val & SUBCACHE_INDEX ) > this_leaf -> l3_indices ))
400+ return - EINVAL ;
401+
402+ val |= BIT (30 );
403+ pci_write_config_dword (dev , 0x1BC + index * 4 , val );
404+ /*
405+ * We need to WBINVD on a core on the node containing the L3 cache which
406+ * indices we disable therefore a simple wbinvd() is not sufficient.
407+ */
408+ wbinvd_on_cpu (cpu );
409+ pci_write_config_dword (dev , 0x1BC + index * 4 , val | BIT (31 ));
410+ return count ;
411+ }
412+
413+ #define STORE_CACHE_DISABLE (index ) \
414+ static ssize_t \
415+ store_cache_disable_##index(struct _cpuid4_info *this_leaf, \
416+ const char *buf, size_t count) \
417+ { \
418+ return store_cache_disable(this_leaf, buf, count, index); \
309419}
420+ STORE_CACHE_DISABLE (0 )
421+ STORE_CACHE_DISABLE (1 )
422+
423+ static struct _cache_attr cache_disable_0 = __ATTR (cache_disable_0 , 0644 ,
424+ show_cache_disable_0 , store_cache_disable_0 );
425+ static struct _cache_attr cache_disable_1 = __ATTR (cache_disable_1 , 0644 ,
426+ show_cache_disable_1 , store_cache_disable_1 );
427+
428+ #else /* CONFIG_CPU_SUP_AMD */
429+ static void __cpuinit
430+ amd_check_l3_disable (int index , struct _cpuid4_info_regs * this_leaf )
431+ {
432+ };
433+ #endif /* CONFIG_CPU_SUP_AMD */
310434
311435static int
312436__cpuinit cpuid4_cache_lookup_regs (int index ,
@@ -713,82 +837,6 @@ static ssize_t show_type(struct _cpuid4_info *this_leaf, char *buf)
713837#define to_object (k ) container_of(k, struct _index_kobject, kobj)
714838#define to_attr (a ) container_of(a, struct _cache_attr, attr)
715839
716- static ssize_t show_cache_disable (struct _cpuid4_info * this_leaf , char * buf ,
717- unsigned int index )
718- {
719- int cpu = cpumask_first (to_cpumask (this_leaf -> shared_cpu_map ));
720- int node = cpu_to_node (cpu );
721- struct pci_dev * dev = node_to_k8_nb_misc (node );
722- unsigned int reg = 0 ;
723-
724- if (!this_leaf -> can_disable )
725- return - EINVAL ;
726-
727- if (!dev )
728- return - EINVAL ;
729-
730- pci_read_config_dword (dev , 0x1BC + index * 4 , & reg );
731- return sprintf (buf , "%x\n" , reg );
732- }
733-
734- #define SHOW_CACHE_DISABLE (index ) \
735- static ssize_t \
736- show_cache_disable_##index(struct _cpuid4_info *this_leaf, char *buf) \
737- { \
738- return show_cache_disable(this_leaf, buf, index); \
739- }
740- SHOW_CACHE_DISABLE (0 )
741- SHOW_CACHE_DISABLE (1 )
742-
743- static ssize_t store_cache_disable (struct _cpuid4_info * this_leaf ,
744- const char * buf , size_t count , unsigned int index )
745- {
746- int cpu = cpumask_first (to_cpumask (this_leaf -> shared_cpu_map ));
747- int node = cpu_to_node (cpu );
748- struct pci_dev * dev = node_to_k8_nb_misc (node );
749- unsigned long val = 0 ;
750- unsigned int scrubber = 0 ;
751-
752- if (!this_leaf -> can_disable )
753- return - EINVAL ;
754-
755- if (!capable (CAP_SYS_ADMIN ))
756- return - EPERM ;
757-
758- if (!dev )
759- return - EINVAL ;
760-
761- if (strict_strtoul (buf , 10 , & val ) < 0 )
762- return - EINVAL ;
763-
764- val |= 0xc0000000 ;
765-
766- pci_read_config_dword (dev , 0x58 , & scrubber );
767- scrubber &= ~0x1f000000 ;
768- pci_write_config_dword (dev , 0x58 , scrubber );
769-
770- pci_write_config_dword (dev , 0x1BC + index * 4 , val & ~0x40000000 );
771- wbinvd ();
772- pci_write_config_dword (dev , 0x1BC + index * 4 , val );
773- return count ;
774- }
775-
776- #define STORE_CACHE_DISABLE (index ) \
777- static ssize_t \
778- store_cache_disable_##index(struct _cpuid4_info *this_leaf, \
779- const char *buf, size_t count) \
780- { \
781- return store_cache_disable(this_leaf, buf, count, index); \
782- }
783- STORE_CACHE_DISABLE (0 )
784- STORE_CACHE_DISABLE (1 )
785-
786- struct _cache_attr {
787- struct attribute attr ;
788- ssize_t (* show )(struct _cpuid4_info * , char * );
789- ssize_t (* store )(struct _cpuid4_info * , const char * , size_t count );
790- };
791-
792840#define define_one_ro (_name ) \
793841static struct _cache_attr _name = \
794842 __ATTR(_name, 0444, show_##_name, NULL)
@@ -803,23 +851,28 @@ define_one_ro(size);
803851define_one_ro (shared_cpu_map );
804852define_one_ro (shared_cpu_list );
805853
806- static struct _cache_attr cache_disable_0 = __ATTR (cache_disable_0 , 0644 ,
807- show_cache_disable_0 , store_cache_disable_0 );
808- static struct _cache_attr cache_disable_1 = __ATTR (cache_disable_1 , 0644 ,
809- show_cache_disable_1 , store_cache_disable_1 );
854+ #define DEFAULT_SYSFS_CACHE_ATTRS \
855+ &type.attr, \
856+ &level.attr, \
857+ &coherency_line_size.attr, \
858+ &physical_line_partition.attr, \
859+ &ways_of_associativity.attr, \
860+ &number_of_sets.attr, \
861+ &size.attr, \
862+ &shared_cpu_map.attr, \
863+ &shared_cpu_list.attr
810864
811865static struct attribute * default_attrs [] = {
812- & type .attr ,
813- & level .attr ,
814- & coherency_line_size .attr ,
815- & physical_line_partition .attr ,
816- & ways_of_associativity .attr ,
817- & number_of_sets .attr ,
818- & size .attr ,
819- & shared_cpu_map .attr ,
820- & shared_cpu_list .attr ,
866+ DEFAULT_SYSFS_CACHE_ATTRS ,
867+ NULL
868+ };
869+
870+ static struct attribute * default_l3_attrs [] = {
871+ DEFAULT_SYSFS_CACHE_ATTRS ,
872+ #ifdef CONFIG_CPU_SUP_AMD
821873 & cache_disable_0 .attr ,
822874 & cache_disable_1 .attr ,
875+ #endif
823876 NULL
824877};
825878
@@ -910,6 +963,7 @@ static int __cpuinit cache_add_dev(struct sys_device * sys_dev)
910963 unsigned int cpu = sys_dev -> id ;
911964 unsigned long i , j ;
912965 struct _index_kobject * this_object ;
966+ struct _cpuid4_info * this_leaf ;
913967 int retval ;
914968
915969 retval = cpuid4_cache_sysfs_init (cpu );
@@ -928,6 +982,14 @@ static int __cpuinit cache_add_dev(struct sys_device * sys_dev)
928982 this_object = INDEX_KOBJECT_PTR (cpu , i );
929983 this_object -> cpu = cpu ;
930984 this_object -> index = i ;
985+
986+ this_leaf = CPUID4_INFO_IDX (cpu , i );
987+
988+ if (this_leaf -> can_disable )
989+ ktype_cache .default_attrs = default_l3_attrs ;
990+ else
991+ ktype_cache .default_attrs = default_attrs ;
992+
931993 retval = kobject_init_and_add (& (this_object -> kobj ),
932994 & ktype_cache ,
933995 per_cpu (ici_cache_kobject , cpu ),
0 commit comments