xref: /OK3568_Linux_fs/kernel/drivers/gpu/arm/mali400/mali/common/mali_user_settings_db.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /**
2  * Copyright (C) 2012-2014, 2016-2017 ARM Limited. All rights reserved.
3  *
4  * This program is free software and is provided to you under the terms of the GNU General Public License version 2
5  * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
6  *
7  * A copy of the licence is included with the program, and can also be obtained from Free Software
8  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
9  */
10 
11 #include "mali_kernel_common.h"
12 #include "mali_osk.h"
13 #include "mali_ukk.h"
14 #include "mali_uk_types.h"
15 #include "mali_user_settings_db.h"
16 #include "mali_session.h"
17 
18 static u32 mali_user_settings[_MALI_UK_USER_SETTING_MAX];
19 const char *_mali_uk_user_setting_descriptions[] = _MALI_UK_USER_SETTING_DESCRIPTIONS;
20 
mali_user_settings_notify(_mali_uk_user_setting_t setting,u32 value)21 static void mali_user_settings_notify(_mali_uk_user_setting_t setting, u32 value)
22 {
23 	mali_bool done = MALI_FALSE;
24 
25 	/*
26 	 * This function gets a bit complicated because we can't hold the session lock while
27 	 * allocating notification objects.
28 	 */
29 
30 	while (!done) {
31 		u32 i;
32 		u32 num_sessions_alloc;
33 		u32 num_sessions_with_lock;
34 		u32 used_notification_objects = 0;
35 		_mali_osk_notification_t **notobjs;
36 
37 		/* Pre allocate the number of notifications objects we need right now (might change after lock has been taken) */
38 		num_sessions_alloc = mali_session_get_count();
39 		if (0 == num_sessions_alloc) {
40 			/* No sessions to report to */
41 			return;
42 		}
43 
44 		notobjs = (_mali_osk_notification_t **)_mali_osk_malloc(sizeof(_mali_osk_notification_t *) * num_sessions_alloc);
45 		if (NULL == notobjs) {
46 			MALI_PRINT_ERROR(("Failed to notify user space session about num PP core change (alloc failure)\n"));
47 			return;
48 		}
49 
50 		for (i = 0; i < num_sessions_alloc; i++) {
51 			notobjs[i] = _mali_osk_notification_create(_MALI_NOTIFICATION_SETTINGS_CHANGED,
52 					sizeof(_mali_uk_settings_changed_s));
53 			if (NULL != notobjs[i]) {
54 				_mali_uk_settings_changed_s *data;
55 				data = notobjs[i]->result_buffer;
56 
57 				data->setting = setting;
58 				data->value = value;
59 			} else {
60 				MALI_PRINT_ERROR(("Failed to notify user space session about setting change (alloc failure %u)\n", i));
61 			}
62 		}
63 
64 		mali_session_lock();
65 
66 		/* number of sessions will not change while we hold the lock */
67 		num_sessions_with_lock = mali_session_get_count();
68 
69 		if (num_sessions_alloc >= num_sessions_with_lock) {
70 			/* We have allocated enough notification objects for all the sessions atm */
71 			struct mali_session_data *session, *tmp;
72 			MALI_SESSION_FOREACH(session, tmp, link) {
73 				MALI_DEBUG_ASSERT(used_notification_objects < num_sessions_alloc);
74 				if (NULL != notobjs[used_notification_objects]) {
75 					mali_session_send_notification(session, notobjs[used_notification_objects]);
76 					notobjs[used_notification_objects] = NULL; /* Don't track this notification object any more */
77 				}
78 				used_notification_objects++;
79 			}
80 			done = MALI_TRUE;
81 		}
82 
83 		mali_session_unlock();
84 
85 		/* Delete any remaining/unused notification objects */
86 		for (; used_notification_objects < num_sessions_alloc; used_notification_objects++) {
87 			if (NULL != notobjs[used_notification_objects]) {
88 				_mali_osk_notification_delete(notobjs[used_notification_objects]);
89 			}
90 		}
91 
92 		_mali_osk_free(notobjs);
93 	}
94 }
95 
mali_set_user_setting(_mali_uk_user_setting_t setting,u32 value)96 void mali_set_user_setting(_mali_uk_user_setting_t setting, u32 value)
97 {
98 	mali_bool notify = MALI_FALSE;
99 
100 	if (setting >= _MALI_UK_USER_SETTING_MAX) {
101 		MALI_DEBUG_PRINT_ERROR(("Invalid user setting %ud\n"));
102 		return;
103 	}
104 
105 	if (mali_user_settings[setting] != value) {
106 		notify = MALI_TRUE;
107 	}
108 
109 	mali_user_settings[setting] = value;
110 
111 	if (notify) {
112 		mali_user_settings_notify(setting, value);
113 	}
114 }
115 
mali_get_user_setting(_mali_uk_user_setting_t setting)116 u32 mali_get_user_setting(_mali_uk_user_setting_t setting)
117 {
118 	if (setting >= _MALI_UK_USER_SETTING_MAX) {
119 		return 0;
120 	}
121 
122 	return mali_user_settings[setting];
123 }
124 
_mali_ukk_get_user_setting(_mali_uk_get_user_setting_s * args)125 _mali_osk_errcode_t _mali_ukk_get_user_setting(_mali_uk_get_user_setting_s *args)
126 {
127 	_mali_uk_user_setting_t setting;
128 	MALI_DEBUG_ASSERT_POINTER(args);
129 
130 	setting = args->setting;
131 
132 	if (_MALI_UK_USER_SETTING_MAX > setting) {
133 		args->value = mali_user_settings[setting];
134 		return _MALI_OSK_ERR_OK;
135 	} else {
136 		return _MALI_OSK_ERR_INVALID_ARGS;
137 	}
138 }
139 
_mali_ukk_get_user_settings(_mali_uk_get_user_settings_s * args)140 _mali_osk_errcode_t _mali_ukk_get_user_settings(_mali_uk_get_user_settings_s *args)
141 {
142 	MALI_DEBUG_ASSERT_POINTER(args);
143 
144 	_mali_osk_memcpy(args->settings, mali_user_settings, sizeof(mali_user_settings));
145 
146 	return _MALI_OSK_ERR_OK;
147 }
148