blob: c870f68ae8f1c3b7b62ce6e55a7b6e7de88bba3b [file] [log] [blame]
Chris Metcalf5c770752011-03-01 13:01:49 -05001/*
2 * Copyright 2011 Tilera Corporation. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
13 * Tilera-specific EDAC driver.
14 *
15 * This source code is derived from the following driver:
16 *
17 * Cell MIC driver for ECC counting
18 *
19 * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
20 * <benh@kernel.crashing.org>
21 *
22 */
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/platform_device.h>
27#include <linux/io.h>
28#include <linux/uaccess.h>
29#include <linux/edac.h>
30#include <hv/hypervisor.h>
31#include <hv/drv_mshim_intf.h>
32
33#include "edac_core.h"
34
35#define DRV_NAME "tile-edac"
36
37/* Number of cs_rows needed per memory controller on TILEPro. */
38#define TILE_EDAC_NR_CSROWS 1
39
40/* Number of channels per memory controller on TILEPro. */
41#define TILE_EDAC_NR_CHANS 1
42
43/* Granularity of reported error in bytes on TILEPro. */
44#define TILE_EDAC_ERROR_GRAIN 8
45
46/* TILE processor has multiple independent memory controllers. */
47struct platform_device *mshim_pdev[TILE_MAX_MSHIMS];
48
49struct tile_edac_priv {
50 int hv_devhdl; /* Hypervisor device handle. */
51 int node; /* Memory controller instance #. */
52 unsigned int ce_count; /*
53 * Correctable-error counter
54 * kept by the driver.
55 */
56};
57
58static void tile_edac_check(struct mem_ctl_info *mci)
59{
60 struct tile_edac_priv *priv = mci->pvt_info;
61 struct mshim_mem_error mem_error;
62
63 if (hv_dev_pread(priv->hv_devhdl, 0, (HV_VirtAddr)&mem_error,
64 sizeof(struct mshim_mem_error), MSHIM_MEM_ERROR_OFF) !=
65 sizeof(struct mshim_mem_error)) {
66 pr_err(DRV_NAME ": MSHIM_MEM_ERROR_OFF pread failure.\n");
67 return;
68 }
69
70 /* Check if the current error count is different from the saved one. */
71 if (mem_error.sbe_count != priv->ce_count) {
72 dev_dbg(mci->dev, "ECC CE err on node %d\n", priv->node);
73 priv->ce_count = mem_error.sbe_count;
74 edac_mc_handle_ce(mci, 0, 0, 0, 0, 0, mci->ctl_name);
75 }
76}
77
78/*
79 * Initialize the 'csrows' table within the mci control structure with the
80 * addressing of memory.
81 */
82static int __devinit tile_edac_init_csrows(struct mem_ctl_info *mci)
83{
84 struct csrow_info *csrow = &mci->csrows[0];
85 struct tile_edac_priv *priv = mci->pvt_info;
86 struct mshim_mem_info mem_info;
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -030087 struct dimm_info *dimm = csrow->channels[0].dimm;
Chris Metcalf5c770752011-03-01 13:01:49 -050088
89 if (hv_dev_pread(priv->hv_devhdl, 0, (HV_VirtAddr)&mem_info,
90 sizeof(struct mshim_mem_info), MSHIM_MEM_INFO_OFF) !=
91 sizeof(struct mshim_mem_info)) {
92 pr_err(DRV_NAME ": MSHIM_MEM_INFO_OFF pread failure.\n");
93 return -1;
94 }
95
96 if (mem_info.mem_ecc)
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -030097 dimm->edac_mode = EDAC_SECDED;
Chris Metcalf5c770752011-03-01 13:01:49 -050098 else
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -030099 dimm->edac_mode = EDAC_NONE;
Chris Metcalf5c770752011-03-01 13:01:49 -0500100 switch (mem_info.mem_type) {
101 case DDR2:
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300102 dimm->mtype = MEM_DDR2;
Chris Metcalf5c770752011-03-01 13:01:49 -0500103 break;
104
105 case DDR3:
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300106 dimm->mtype = MEM_DDR3;
Chris Metcalf5c770752011-03-01 13:01:49 -0500107 break;
108
109 default:
110 return -1;
111 }
112
113 csrow->first_page = 0;
114 csrow->nr_pages = mem_info.mem_size >> PAGE_SHIFT;
115 csrow->last_page = csrow->first_page + csrow->nr_pages - 1;
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300116 dimm->grain = TILE_EDAC_ERROR_GRAIN;
117 dimm->dtype = DEV_UNKNOWN;
Chris Metcalf5c770752011-03-01 13:01:49 -0500118
119 return 0;
120}
121
122static int __devinit tile_edac_mc_probe(struct platform_device *pdev)
123{
124 char hv_file[32];
125 int hv_devhdl;
126 struct mem_ctl_info *mci;
127 struct tile_edac_priv *priv;
128 int rc;
129
130 sprintf(hv_file, "mshim/%d", pdev->id);
131 hv_devhdl = hv_dev_open((HV_VirtAddr)hv_file, 0);
132 if (hv_devhdl < 0)
133 return -EINVAL;
134
135 /* A TILE MC has a single channel and one chip-select row. */
136 mci = edac_mc_alloc(sizeof(struct tile_edac_priv),
137 TILE_EDAC_NR_CSROWS, TILE_EDAC_NR_CHANS, pdev->id);
138 if (mci == NULL)
139 return -ENOMEM;
140 priv = mci->pvt_info;
141 priv->node = pdev->id;
142 priv->hv_devhdl = hv_devhdl;
143
144 mci->dev = &pdev->dev;
145 mci->mtype_cap = MEM_FLAG_DDR2;
146 mci->edac_ctl_cap = EDAC_FLAG_SECDED;
147
148 mci->mod_name = DRV_NAME;
Chris Metcalfe2e110d2012-03-30 18:58:37 -0400149#ifdef __tilegx__
150 mci->ctl_name = "TILEGx_Memory_Controller";
151#else
Chris Metcalf5c770752011-03-01 13:01:49 -0500152 mci->ctl_name = "TILEPro_Memory_Controller";
Chris Metcalfe2e110d2012-03-30 18:58:37 -0400153#endif
Chris Metcalf5c770752011-03-01 13:01:49 -0500154 mci->dev_name = dev_name(&pdev->dev);
155 mci->edac_check = tile_edac_check;
156
157 /*
158 * Initialize the MC control structure 'csrows' table
159 * with the mapping and control information.
160 */
161 if (tile_edac_init_csrows(mci)) {
162 /* No csrows found. */
163 mci->edac_cap = EDAC_FLAG_NONE;
164 } else {
165 mci->edac_cap = EDAC_FLAG_SECDED;
166 }
167
168 platform_set_drvdata(pdev, mci);
169
170 /* Register with EDAC core */
171 rc = edac_mc_add_mc(mci);
172 if (rc) {
173 dev_err(&pdev->dev, "failed to register with EDAC core\n");
174 edac_mc_free(mci);
175 return rc;
176 }
177
178 return 0;
179}
180
181static int __devexit tile_edac_mc_remove(struct platform_device *pdev)
182{
183 struct mem_ctl_info *mci = platform_get_drvdata(pdev);
184
185 edac_mc_del_mc(&pdev->dev);
186 if (mci)
187 edac_mc_free(mci);
188 return 0;
189}
190
191static struct platform_driver tile_edac_mc_driver = {
192 .driver = {
193 .name = DRV_NAME,
194 .owner = THIS_MODULE,
195 },
196 .probe = tile_edac_mc_probe,
197 .remove = __devexit_p(tile_edac_mc_remove),
198};
199
200/*
201 * Driver init routine.
202 */
203static int __init tile_edac_init(void)
204{
205 char hv_file[32];
206 struct platform_device *pdev;
207 int i, err, num = 0;
208
209 /* Only support POLL mode. */
210 edac_op_state = EDAC_OPSTATE_POLL;
211
212 err = platform_driver_register(&tile_edac_mc_driver);
213 if (err)
214 return err;
215
216 for (i = 0; i < TILE_MAX_MSHIMS; i++) {
217 /*
218 * Not all memory controllers are configured such as in the
219 * case of a simulator. So we register only those mshims
220 * that are configured by the hypervisor.
221 */
222 sprintf(hv_file, "mshim/%d", i);
223 if (hv_dev_open((HV_VirtAddr)hv_file, 0) < 0)
224 continue;
225
226 pdev = platform_device_register_simple(DRV_NAME, i, NULL, 0);
227 if (IS_ERR(pdev))
228 continue;
229 mshim_pdev[i] = pdev;
230 num++;
231 }
232
233 if (num == 0) {
234 platform_driver_unregister(&tile_edac_mc_driver);
235 return -ENODEV;
236 }
237 return 0;
238}
239
240/*
241 * Driver cleanup routine.
242 */
243static void __exit tile_edac_exit(void)
244{
245 int i;
246
247 for (i = 0; i < TILE_MAX_MSHIMS; i++) {
248 struct platform_device *pdev = mshim_pdev[i];
249 if (!pdev)
250 continue;
251
252 platform_set_drvdata(pdev, NULL);
253 platform_device_unregister(pdev);
254 }
255 platform_driver_unregister(&tile_edac_mc_driver);
256}
257
258module_init(tile_edac_init);
259module_exit(tile_edac_exit);