i915_gem.c 137 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332
  1. /*
  2. * Copyright © 2008-2015 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/drm_vma_manager.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_vgpu.h"
  32. #include "i915_trace.h"
  33. #include "intel_drv.h"
  34. #include "intel_mocs.h"
  35. #include <linux/shmem_fs.h>
  36. #include <linux/slab.h>
  37. #include <linux/swap.h>
  38. #include <linux/pci.h>
  39. #include <linux/dma-buf.h>
  40. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  41. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  42. static void
  43. i915_gem_object_retire__write(struct drm_i915_gem_object *obj);
  44. static void
  45. i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring);
  46. static bool cpu_cache_is_coherent(struct drm_device *dev,
  47. enum i915_cache_level level)
  48. {
  49. return HAS_LLC(dev) || level != I915_CACHE_NONE;
  50. }
  51. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  52. {
  53. if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  54. return true;
  55. return obj->pin_display;
  56. }
  57. /* some bookkeeping */
  58. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  59. size_t size)
  60. {
  61. spin_lock(&dev_priv->mm.object_stat_lock);
  62. dev_priv->mm.object_count++;
  63. dev_priv->mm.object_memory += size;
  64. spin_unlock(&dev_priv->mm.object_stat_lock);
  65. }
  66. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  67. size_t size)
  68. {
  69. spin_lock(&dev_priv->mm.object_stat_lock);
  70. dev_priv->mm.object_count--;
  71. dev_priv->mm.object_memory -= size;
  72. spin_unlock(&dev_priv->mm.object_stat_lock);
  73. }
  74. static int
  75. i915_gem_wait_for_error(struct i915_gpu_error *error)
  76. {
  77. int ret;
  78. if (!i915_reset_in_progress(error))
  79. return 0;
  80. /*
  81. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  82. * userspace. If it takes that long something really bad is going on and
  83. * we should simply try to bail out and fail as gracefully as possible.
  84. */
  85. ret = wait_event_interruptible_timeout(error->reset_queue,
  86. !i915_reset_in_progress(error),
  87. 10*HZ);
  88. if (ret == 0) {
  89. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  90. return -EIO;
  91. } else if (ret < 0) {
  92. return ret;
  93. } else {
  94. return 0;
  95. }
  96. }
  97. int i915_mutex_lock_interruptible(struct drm_device *dev)
  98. {
  99. struct drm_i915_private *dev_priv = dev->dev_private;
  100. int ret;
  101. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  102. if (ret)
  103. return ret;
  104. ret = mutex_lock_interruptible(&dev->struct_mutex);
  105. if (ret)
  106. return ret;
  107. WARN_ON(i915_verify_lists(dev));
  108. return 0;
  109. }
  110. int
  111. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  112. struct drm_file *file)
  113. {
  114. struct drm_i915_private *dev_priv = to_i915(dev);
  115. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  116. struct drm_i915_gem_get_aperture *args = data;
  117. struct i915_vma *vma;
  118. size_t pinned;
  119. pinned = 0;
  120. mutex_lock(&dev->struct_mutex);
  121. list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
  122. if (vma->pin_count)
  123. pinned += vma->node.size;
  124. list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
  125. if (vma->pin_count)
  126. pinned += vma->node.size;
  127. mutex_unlock(&dev->struct_mutex);
  128. args->aper_size = ggtt->base.total;
  129. args->aper_available_size = args->aper_size - pinned;
  130. return 0;
  131. }
  132. static int
  133. i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
  134. {
  135. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  136. char *vaddr = obj->phys_handle->vaddr;
  137. struct sg_table *st;
  138. struct scatterlist *sg;
  139. int i;
  140. if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
  141. return -EINVAL;
  142. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  143. struct page *page;
  144. char *src;
  145. page = shmem_read_mapping_page(mapping, i);
  146. if (IS_ERR(page))
  147. return PTR_ERR(page);
  148. src = kmap_atomic(page);
  149. memcpy(vaddr, src, PAGE_SIZE);
  150. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  151. kunmap_atomic(src);
  152. put_page(page);
  153. vaddr += PAGE_SIZE;
  154. }
  155. i915_gem_chipset_flush(to_i915(obj->base.dev));
  156. st = kmalloc(sizeof(*st), GFP_KERNEL);
  157. if (st == NULL)
  158. return -ENOMEM;
  159. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  160. kfree(st);
  161. return -ENOMEM;
  162. }
  163. sg = st->sgl;
  164. sg->offset = 0;
  165. sg->length = obj->base.size;
  166. sg_dma_address(sg) = obj->phys_handle->busaddr;
  167. sg_dma_len(sg) = obj->base.size;
  168. obj->pages = st;
  169. return 0;
  170. }
  171. static void
  172. i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj)
  173. {
  174. int ret;
  175. BUG_ON(obj->madv == __I915_MADV_PURGED);
  176. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  177. if (WARN_ON(ret)) {
  178. /* In the event of a disaster, abandon all caches and
  179. * hope for the best.
  180. */
  181. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  182. }
  183. if (obj->madv == I915_MADV_DONTNEED)
  184. obj->dirty = 0;
  185. if (obj->dirty) {
  186. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  187. char *vaddr = obj->phys_handle->vaddr;
  188. int i;
  189. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  190. struct page *page;
  191. char *dst;
  192. page = shmem_read_mapping_page(mapping, i);
  193. if (IS_ERR(page))
  194. continue;
  195. dst = kmap_atomic(page);
  196. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  197. memcpy(dst, vaddr, PAGE_SIZE);
  198. kunmap_atomic(dst);
  199. set_page_dirty(page);
  200. if (obj->madv == I915_MADV_WILLNEED)
  201. mark_page_accessed(page);
  202. put_page(page);
  203. vaddr += PAGE_SIZE;
  204. }
  205. obj->dirty = 0;
  206. }
  207. sg_free_table(obj->pages);
  208. kfree(obj->pages);
  209. }
  210. static void
  211. i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
  212. {
  213. drm_pci_free(obj->base.dev, obj->phys_handle);
  214. }
  215. static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
  216. .get_pages = i915_gem_object_get_pages_phys,
  217. .put_pages = i915_gem_object_put_pages_phys,
  218. .release = i915_gem_object_release_phys,
  219. };
  220. static int
  221. drop_pages(struct drm_i915_gem_object *obj)
  222. {
  223. struct i915_vma *vma, *next;
  224. int ret;
  225. drm_gem_object_reference(&obj->base);
  226. list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link)
  227. if (i915_vma_unbind(vma))
  228. break;
  229. ret = i915_gem_object_put_pages(obj);
  230. drm_gem_object_unreference(&obj->base);
  231. return ret;
  232. }
  233. int
  234. i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
  235. int align)
  236. {
  237. drm_dma_handle_t *phys;
  238. int ret;
  239. if (obj->phys_handle) {
  240. if ((unsigned long)obj->phys_handle->vaddr & (align -1))
  241. return -EBUSY;
  242. return 0;
  243. }
  244. if (obj->madv != I915_MADV_WILLNEED)
  245. return -EFAULT;
  246. if (obj->base.filp == NULL)
  247. return -EINVAL;
  248. ret = drop_pages(obj);
  249. if (ret)
  250. return ret;
  251. /* create a new object */
  252. phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
  253. if (!phys)
  254. return -ENOMEM;
  255. obj->phys_handle = phys;
  256. obj->ops = &i915_gem_phys_ops;
  257. return i915_gem_object_get_pages(obj);
  258. }
  259. static int
  260. i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
  261. struct drm_i915_gem_pwrite *args,
  262. struct drm_file *file_priv)
  263. {
  264. struct drm_device *dev = obj->base.dev;
  265. void *vaddr = obj->phys_handle->vaddr + args->offset;
  266. char __user *user_data = u64_to_user_ptr(args->data_ptr);
  267. int ret = 0;
  268. /* We manually control the domain here and pretend that it
  269. * remains coherent i.e. in the GTT domain, like shmem_pwrite.
  270. */
  271. ret = i915_gem_object_wait_rendering(obj, false);
  272. if (ret)
  273. return ret;
  274. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  275. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  276. unsigned long unwritten;
  277. /* The physical object once assigned is fixed for the lifetime
  278. * of the obj, so we can safely drop the lock and continue
  279. * to access vaddr.
  280. */
  281. mutex_unlock(&dev->struct_mutex);
  282. unwritten = copy_from_user(vaddr, user_data, args->size);
  283. mutex_lock(&dev->struct_mutex);
  284. if (unwritten) {
  285. ret = -EFAULT;
  286. goto out;
  287. }
  288. }
  289. drm_clflush_virt_range(vaddr, args->size);
  290. i915_gem_chipset_flush(to_i915(dev));
  291. out:
  292. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  293. return ret;
  294. }
  295. void *i915_gem_object_alloc(struct drm_device *dev)
  296. {
  297. struct drm_i915_private *dev_priv = dev->dev_private;
  298. return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
  299. }
  300. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  301. {
  302. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  303. kmem_cache_free(dev_priv->objects, obj);
  304. }
  305. static int
  306. i915_gem_create(struct drm_file *file,
  307. struct drm_device *dev,
  308. uint64_t size,
  309. uint32_t *handle_p)
  310. {
  311. struct drm_i915_gem_object *obj;
  312. int ret;
  313. u32 handle;
  314. size = roundup(size, PAGE_SIZE);
  315. if (size == 0)
  316. return -EINVAL;
  317. /* Allocate the new object */
  318. obj = i915_gem_object_create(dev, size);
  319. if (IS_ERR(obj))
  320. return PTR_ERR(obj);
  321. ret = drm_gem_handle_create(file, &obj->base, &handle);
  322. /* drop reference from allocate - handle holds it now */
  323. drm_gem_object_unreference_unlocked(&obj->base);
  324. if (ret)
  325. return ret;
  326. *handle_p = handle;
  327. return 0;
  328. }
  329. int
  330. i915_gem_dumb_create(struct drm_file *file,
  331. struct drm_device *dev,
  332. struct drm_mode_create_dumb *args)
  333. {
  334. /* have to work out size/pitch and return them */
  335. args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
  336. args->size = args->pitch * args->height;
  337. return i915_gem_create(file, dev,
  338. args->size, &args->handle);
  339. }
  340. /**
  341. * Creates a new mm object and returns a handle to it.
  342. */
  343. int
  344. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  345. struct drm_file *file)
  346. {
  347. struct drm_i915_gem_create *args = data;
  348. return i915_gem_create(file, dev,
  349. args->size, &args->handle);
  350. }
  351. static inline int
  352. __copy_to_user_swizzled(char __user *cpu_vaddr,
  353. const char *gpu_vaddr, int gpu_offset,
  354. int length)
  355. {
  356. int ret, cpu_offset = 0;
  357. while (length > 0) {
  358. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  359. int this_length = min(cacheline_end - gpu_offset, length);
  360. int swizzled_gpu_offset = gpu_offset ^ 64;
  361. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  362. gpu_vaddr + swizzled_gpu_offset,
  363. this_length);
  364. if (ret)
  365. return ret + length;
  366. cpu_offset += this_length;
  367. gpu_offset += this_length;
  368. length -= this_length;
  369. }
  370. return 0;
  371. }
  372. static inline int
  373. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  374. const char __user *cpu_vaddr,
  375. int length)
  376. {
  377. int ret, cpu_offset = 0;
  378. while (length > 0) {
  379. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  380. int this_length = min(cacheline_end - gpu_offset, length);
  381. int swizzled_gpu_offset = gpu_offset ^ 64;
  382. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  383. cpu_vaddr + cpu_offset,
  384. this_length);
  385. if (ret)
  386. return ret + length;
  387. cpu_offset += this_length;
  388. gpu_offset += this_length;
  389. length -= this_length;
  390. }
  391. return 0;
  392. }
  393. /*
  394. * Pins the specified object's pages and synchronizes the object with
  395. * GPU accesses. Sets needs_clflush to non-zero if the caller should
  396. * flush the object from the CPU cache.
  397. */
  398. int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
  399. int *needs_clflush)
  400. {
  401. int ret;
  402. *needs_clflush = 0;
  403. if (WARN_ON((obj->ops->flags & I915_GEM_OBJECT_HAS_STRUCT_PAGE) == 0))
  404. return -EINVAL;
  405. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
  406. /* If we're not in the cpu read domain, set ourself into the gtt
  407. * read domain and manually flush cachelines (if required). This
  408. * optimizes for the case when the gpu will dirty the data
  409. * anyway again before the next pread happens. */
  410. *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
  411. obj->cache_level);
  412. ret = i915_gem_object_wait_rendering(obj, true);
  413. if (ret)
  414. return ret;
  415. }
  416. ret = i915_gem_object_get_pages(obj);
  417. if (ret)
  418. return ret;
  419. i915_gem_object_pin_pages(obj);
  420. return ret;
  421. }
  422. /* Per-page copy function for the shmem pread fastpath.
  423. * Flushes invalid cachelines before reading the target if
  424. * needs_clflush is set. */
  425. static int
  426. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  427. char __user *user_data,
  428. bool page_do_bit17_swizzling, bool needs_clflush)
  429. {
  430. char *vaddr;
  431. int ret;
  432. if (unlikely(page_do_bit17_swizzling))
  433. return -EINVAL;
  434. vaddr = kmap_atomic(page);
  435. if (needs_clflush)
  436. drm_clflush_virt_range(vaddr + shmem_page_offset,
  437. page_length);
  438. ret = __copy_to_user_inatomic(user_data,
  439. vaddr + shmem_page_offset,
  440. page_length);
  441. kunmap_atomic(vaddr);
  442. return ret ? -EFAULT : 0;
  443. }
  444. static void
  445. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  446. bool swizzled)
  447. {
  448. if (unlikely(swizzled)) {
  449. unsigned long start = (unsigned long) addr;
  450. unsigned long end = (unsigned long) addr + length;
  451. /* For swizzling simply ensure that we always flush both
  452. * channels. Lame, but simple and it works. Swizzled
  453. * pwrite/pread is far from a hotpath - current userspace
  454. * doesn't use it at all. */
  455. start = round_down(start, 128);
  456. end = round_up(end, 128);
  457. drm_clflush_virt_range((void *)start, end - start);
  458. } else {
  459. drm_clflush_virt_range(addr, length);
  460. }
  461. }
  462. /* Only difference to the fast-path function is that this can handle bit17
  463. * and uses non-atomic copy and kmap functions. */
  464. static int
  465. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  466. char __user *user_data,
  467. bool page_do_bit17_swizzling, bool needs_clflush)
  468. {
  469. char *vaddr;
  470. int ret;
  471. vaddr = kmap(page);
  472. if (needs_clflush)
  473. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  474. page_length,
  475. page_do_bit17_swizzling);
  476. if (page_do_bit17_swizzling)
  477. ret = __copy_to_user_swizzled(user_data,
  478. vaddr, shmem_page_offset,
  479. page_length);
  480. else
  481. ret = __copy_to_user(user_data,
  482. vaddr + shmem_page_offset,
  483. page_length);
  484. kunmap(page);
  485. return ret ? - EFAULT : 0;
  486. }
  487. static int
  488. i915_gem_shmem_pread(struct drm_device *dev,
  489. struct drm_i915_gem_object *obj,
  490. struct drm_i915_gem_pread *args,
  491. struct drm_file *file)
  492. {
  493. char __user *user_data;
  494. ssize_t remain;
  495. loff_t offset;
  496. int shmem_page_offset, page_length, ret = 0;
  497. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  498. int prefaulted = 0;
  499. int needs_clflush = 0;
  500. struct sg_page_iter sg_iter;
  501. user_data = u64_to_user_ptr(args->data_ptr);
  502. remain = args->size;
  503. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  504. ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
  505. if (ret)
  506. return ret;
  507. offset = args->offset;
  508. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  509. offset >> PAGE_SHIFT) {
  510. struct page *page = sg_page_iter_page(&sg_iter);
  511. if (remain <= 0)
  512. break;
  513. /* Operation in this page
  514. *
  515. * shmem_page_offset = offset within page in shmem file
  516. * page_length = bytes to copy for this page
  517. */
  518. shmem_page_offset = offset_in_page(offset);
  519. page_length = remain;
  520. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  521. page_length = PAGE_SIZE - shmem_page_offset;
  522. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  523. (page_to_phys(page) & (1 << 17)) != 0;
  524. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  525. user_data, page_do_bit17_swizzling,
  526. needs_clflush);
  527. if (ret == 0)
  528. goto next_page;
  529. mutex_unlock(&dev->struct_mutex);
  530. if (likely(!i915.prefault_disable) && !prefaulted) {
  531. ret = fault_in_multipages_writeable(user_data, remain);
  532. /* Userspace is tricking us, but we've already clobbered
  533. * its pages with the prefault and promised to write the
  534. * data up to the first fault. Hence ignore any errors
  535. * and just continue. */
  536. (void)ret;
  537. prefaulted = 1;
  538. }
  539. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  540. user_data, page_do_bit17_swizzling,
  541. needs_clflush);
  542. mutex_lock(&dev->struct_mutex);
  543. if (ret)
  544. goto out;
  545. next_page:
  546. remain -= page_length;
  547. user_data += page_length;
  548. offset += page_length;
  549. }
  550. out:
  551. i915_gem_object_unpin_pages(obj);
  552. return ret;
  553. }
  554. /**
  555. * Reads data from the object referenced by handle.
  556. *
  557. * On error, the contents of *data are undefined.
  558. */
  559. int
  560. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  561. struct drm_file *file)
  562. {
  563. struct drm_i915_gem_pread *args = data;
  564. struct drm_i915_gem_object *obj;
  565. int ret = 0;
  566. if (args->size == 0)
  567. return 0;
  568. if (!access_ok(VERIFY_WRITE,
  569. u64_to_user_ptr(args->data_ptr),
  570. args->size))
  571. return -EFAULT;
  572. ret = i915_mutex_lock_interruptible(dev);
  573. if (ret)
  574. return ret;
  575. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  576. if (&obj->base == NULL) {
  577. ret = -ENOENT;
  578. goto unlock;
  579. }
  580. /* Bounds check source. */
  581. if (args->offset > obj->base.size ||
  582. args->size > obj->base.size - args->offset) {
  583. ret = -EINVAL;
  584. goto out;
  585. }
  586. /* prime objects have no backing filp to GEM pread/pwrite
  587. * pages from.
  588. */
  589. if (!obj->base.filp) {
  590. ret = -EINVAL;
  591. goto out;
  592. }
  593. trace_i915_gem_object_pread(obj, args->offset, args->size);
  594. ret = i915_gem_shmem_pread(dev, obj, args, file);
  595. out:
  596. drm_gem_object_unreference(&obj->base);
  597. unlock:
  598. mutex_unlock(&dev->struct_mutex);
  599. return ret;
  600. }
  601. /* This is the fast write path which cannot handle
  602. * page faults in the source data
  603. */
  604. static inline int
  605. fast_user_write(struct io_mapping *mapping,
  606. loff_t page_base, int page_offset,
  607. char __user *user_data,
  608. int length)
  609. {
  610. void __iomem *vaddr_atomic;
  611. void *vaddr;
  612. unsigned long unwritten;
  613. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  614. /* We can use the cpu mem copy function because this is X86. */
  615. vaddr = (void __force*)vaddr_atomic + page_offset;
  616. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  617. user_data, length);
  618. io_mapping_unmap_atomic(vaddr_atomic);
  619. return unwritten;
  620. }
  621. /**
  622. * This is the fast pwrite path, where we copy the data directly from the
  623. * user into the GTT, uncached.
  624. */
  625. static int
  626. i915_gem_gtt_pwrite_fast(struct drm_device *dev,
  627. struct drm_i915_gem_object *obj,
  628. struct drm_i915_gem_pwrite *args,
  629. struct drm_file *file)
  630. {
  631. struct drm_i915_private *dev_priv = to_i915(dev);
  632. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  633. ssize_t remain;
  634. loff_t offset, page_base;
  635. char __user *user_data;
  636. int page_offset, page_length, ret;
  637. ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
  638. if (ret)
  639. goto out;
  640. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  641. if (ret)
  642. goto out_unpin;
  643. ret = i915_gem_object_put_fence(obj);
  644. if (ret)
  645. goto out_unpin;
  646. user_data = u64_to_user_ptr(args->data_ptr);
  647. remain = args->size;
  648. offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
  649. intel_fb_obj_invalidate(obj, ORIGIN_GTT);
  650. while (remain > 0) {
  651. /* Operation in this page
  652. *
  653. * page_base = page offset within aperture
  654. * page_offset = offset within page
  655. * page_length = bytes to copy for this page
  656. */
  657. page_base = offset & PAGE_MASK;
  658. page_offset = offset_in_page(offset);
  659. page_length = remain;
  660. if ((page_offset + remain) > PAGE_SIZE)
  661. page_length = PAGE_SIZE - page_offset;
  662. /* If we get a fault while copying data, then (presumably) our
  663. * source page isn't available. Return the error and we'll
  664. * retry in the slow path.
  665. */
  666. if (fast_user_write(ggtt->mappable, page_base,
  667. page_offset, user_data, page_length)) {
  668. ret = -EFAULT;
  669. goto out_flush;
  670. }
  671. remain -= page_length;
  672. user_data += page_length;
  673. offset += page_length;
  674. }
  675. out_flush:
  676. intel_fb_obj_flush(obj, false, ORIGIN_GTT);
  677. out_unpin:
  678. i915_gem_object_ggtt_unpin(obj);
  679. out:
  680. return ret;
  681. }
  682. /* Per-page copy function for the shmem pwrite fastpath.
  683. * Flushes invalid cachelines before writing to the target if
  684. * needs_clflush_before is set and flushes out any written cachelines after
  685. * writing if needs_clflush is set. */
  686. static int
  687. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  688. char __user *user_data,
  689. bool page_do_bit17_swizzling,
  690. bool needs_clflush_before,
  691. bool needs_clflush_after)
  692. {
  693. char *vaddr;
  694. int ret;
  695. if (unlikely(page_do_bit17_swizzling))
  696. return -EINVAL;
  697. vaddr = kmap_atomic(page);
  698. if (needs_clflush_before)
  699. drm_clflush_virt_range(vaddr + shmem_page_offset,
  700. page_length);
  701. ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
  702. user_data, page_length);
  703. if (needs_clflush_after)
  704. drm_clflush_virt_range(vaddr + shmem_page_offset,
  705. page_length);
  706. kunmap_atomic(vaddr);
  707. return ret ? -EFAULT : 0;
  708. }
  709. /* Only difference to the fast-path function is that this can handle bit17
  710. * and uses non-atomic copy and kmap functions. */
  711. static int
  712. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  713. char __user *user_data,
  714. bool page_do_bit17_swizzling,
  715. bool needs_clflush_before,
  716. bool needs_clflush_after)
  717. {
  718. char *vaddr;
  719. int ret;
  720. vaddr = kmap(page);
  721. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  722. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  723. page_length,
  724. page_do_bit17_swizzling);
  725. if (page_do_bit17_swizzling)
  726. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  727. user_data,
  728. page_length);
  729. else
  730. ret = __copy_from_user(vaddr + shmem_page_offset,
  731. user_data,
  732. page_length);
  733. if (needs_clflush_after)
  734. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  735. page_length,
  736. page_do_bit17_swizzling);
  737. kunmap(page);
  738. return ret ? -EFAULT : 0;
  739. }
  740. static int
  741. i915_gem_shmem_pwrite(struct drm_device *dev,
  742. struct drm_i915_gem_object *obj,
  743. struct drm_i915_gem_pwrite *args,
  744. struct drm_file *file)
  745. {
  746. ssize_t remain;
  747. loff_t offset;
  748. char __user *user_data;
  749. int shmem_page_offset, page_length, ret = 0;
  750. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  751. int hit_slowpath = 0;
  752. int needs_clflush_after = 0;
  753. int needs_clflush_before = 0;
  754. struct sg_page_iter sg_iter;
  755. user_data = u64_to_user_ptr(args->data_ptr);
  756. remain = args->size;
  757. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  758. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  759. /* If we're not in the cpu write domain, set ourself into the gtt
  760. * write domain and manually flush cachelines (if required). This
  761. * optimizes for the case when the gpu will use the data
  762. * right away and we therefore have to clflush anyway. */
  763. needs_clflush_after = cpu_write_needs_clflush(obj);
  764. ret = i915_gem_object_wait_rendering(obj, false);
  765. if (ret)
  766. return ret;
  767. }
  768. /* Same trick applies to invalidate partially written cachelines read
  769. * before writing. */
  770. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
  771. needs_clflush_before =
  772. !cpu_cache_is_coherent(dev, obj->cache_level);
  773. ret = i915_gem_object_get_pages(obj);
  774. if (ret)
  775. return ret;
  776. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  777. i915_gem_object_pin_pages(obj);
  778. offset = args->offset;
  779. obj->dirty = 1;
  780. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  781. offset >> PAGE_SHIFT) {
  782. struct page *page = sg_page_iter_page(&sg_iter);
  783. int partial_cacheline_write;
  784. if (remain <= 0)
  785. break;
  786. /* Operation in this page
  787. *
  788. * shmem_page_offset = offset within page in shmem file
  789. * page_length = bytes to copy for this page
  790. */
  791. shmem_page_offset = offset_in_page(offset);
  792. page_length = remain;
  793. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  794. page_length = PAGE_SIZE - shmem_page_offset;
  795. /* If we don't overwrite a cacheline completely we need to be
  796. * careful to have up-to-date data by first clflushing. Don't
  797. * overcomplicate things and flush the entire patch. */
  798. partial_cacheline_write = needs_clflush_before &&
  799. ((shmem_page_offset | page_length)
  800. & (boot_cpu_data.x86_clflush_size - 1));
  801. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  802. (page_to_phys(page) & (1 << 17)) != 0;
  803. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  804. user_data, page_do_bit17_swizzling,
  805. partial_cacheline_write,
  806. needs_clflush_after);
  807. if (ret == 0)
  808. goto next_page;
  809. hit_slowpath = 1;
  810. mutex_unlock(&dev->struct_mutex);
  811. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  812. user_data, page_do_bit17_swizzling,
  813. partial_cacheline_write,
  814. needs_clflush_after);
  815. mutex_lock(&dev->struct_mutex);
  816. if (ret)
  817. goto out;
  818. next_page:
  819. remain -= page_length;
  820. user_data += page_length;
  821. offset += page_length;
  822. }
  823. out:
  824. i915_gem_object_unpin_pages(obj);
  825. if (hit_slowpath) {
  826. /*
  827. * Fixup: Flush cpu caches in case we didn't flush the dirty
  828. * cachelines in-line while writing and the object moved
  829. * out of the cpu write domain while we've dropped the lock.
  830. */
  831. if (!needs_clflush_after &&
  832. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  833. if (i915_gem_clflush_object(obj, obj->pin_display))
  834. needs_clflush_after = true;
  835. }
  836. }
  837. if (needs_clflush_after)
  838. i915_gem_chipset_flush(to_i915(dev));
  839. else
  840. obj->cache_dirty = true;
  841. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  842. return ret;
  843. }
  844. /**
  845. * Writes data to the object referenced by handle.
  846. *
  847. * On error, the contents of the buffer that were to be modified are undefined.
  848. */
  849. int
  850. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  851. struct drm_file *file)
  852. {
  853. struct drm_i915_private *dev_priv = dev->dev_private;
  854. struct drm_i915_gem_pwrite *args = data;
  855. struct drm_i915_gem_object *obj;
  856. int ret;
  857. if (args->size == 0)
  858. return 0;
  859. if (!access_ok(VERIFY_READ,
  860. u64_to_user_ptr(args->data_ptr),
  861. args->size))
  862. return -EFAULT;
  863. if (likely(!i915.prefault_disable)) {
  864. ret = fault_in_multipages_readable(u64_to_user_ptr(args->data_ptr),
  865. args->size);
  866. if (ret)
  867. return -EFAULT;
  868. }
  869. intel_runtime_pm_get(dev_priv);
  870. ret = i915_mutex_lock_interruptible(dev);
  871. if (ret)
  872. goto put_rpm;
  873. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  874. if (&obj->base == NULL) {
  875. ret = -ENOENT;
  876. goto unlock;
  877. }
  878. /* Bounds check destination. */
  879. if (args->offset > obj->base.size ||
  880. args->size > obj->base.size - args->offset) {
  881. ret = -EINVAL;
  882. goto out;
  883. }
  884. /* prime objects have no backing filp to GEM pread/pwrite
  885. * pages from.
  886. */
  887. if (!obj->base.filp) {
  888. ret = -EINVAL;
  889. goto out;
  890. }
  891. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  892. ret = -EFAULT;
  893. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  894. * it would end up going through the fenced access, and we'll get
  895. * different detiling behavior between reading and writing.
  896. * pread/pwrite currently are reading and writing from the CPU
  897. * perspective, requiring manual detiling by the client.
  898. */
  899. if (obj->tiling_mode == I915_TILING_NONE &&
  900. obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
  901. cpu_write_needs_clflush(obj)) {
  902. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
  903. /* Note that the gtt paths might fail with non-page-backed user
  904. * pointers (e.g. gtt mappings when moving data between
  905. * textures). Fallback to the shmem path in that case. */
  906. }
  907. if (ret == -EFAULT || ret == -ENOSPC) {
  908. if (obj->phys_handle)
  909. ret = i915_gem_phys_pwrite(obj, args, file);
  910. else
  911. ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  912. }
  913. out:
  914. drm_gem_object_unreference(&obj->base);
  915. unlock:
  916. mutex_unlock(&dev->struct_mutex);
  917. put_rpm:
  918. intel_runtime_pm_put(dev_priv);
  919. return ret;
  920. }
  921. static int
  922. i915_gem_check_wedge(unsigned reset_counter, bool interruptible)
  923. {
  924. if (__i915_terminally_wedged(reset_counter))
  925. return -EIO;
  926. if (__i915_reset_in_progress(reset_counter)) {
  927. /* Non-interruptible callers can't handle -EAGAIN, hence return
  928. * -EIO unconditionally for these. */
  929. if (!interruptible)
  930. return -EIO;
  931. return -EAGAIN;
  932. }
  933. return 0;
  934. }
  935. static void fake_irq(unsigned long data)
  936. {
  937. wake_up_process((struct task_struct *)data);
  938. }
  939. static bool missed_irq(struct drm_i915_private *dev_priv,
  940. struct intel_engine_cs *engine)
  941. {
  942. return test_bit(engine->id, &dev_priv->gpu_error.missed_irq_rings);
  943. }
  944. static unsigned long local_clock_us(unsigned *cpu)
  945. {
  946. unsigned long t;
  947. /* Cheaply and approximately convert from nanoseconds to microseconds.
  948. * The result and subsequent calculations are also defined in the same
  949. * approximate microseconds units. The principal source of timing
  950. * error here is from the simple truncation.
  951. *
  952. * Note that local_clock() is only defined wrt to the current CPU;
  953. * the comparisons are no longer valid if we switch CPUs. Instead of
  954. * blocking preemption for the entire busywait, we can detect the CPU
  955. * switch and use that as indicator of system load and a reason to
  956. * stop busywaiting, see busywait_stop().
  957. */
  958. *cpu = get_cpu();
  959. t = local_clock() >> 10;
  960. put_cpu();
  961. return t;
  962. }
  963. static bool busywait_stop(unsigned long timeout, unsigned cpu)
  964. {
  965. unsigned this_cpu;
  966. if (time_after(local_clock_us(&this_cpu), timeout))
  967. return true;
  968. return this_cpu != cpu;
  969. }
  970. static int __i915_spin_request(struct drm_i915_gem_request *req, int state)
  971. {
  972. unsigned long timeout;
  973. unsigned cpu;
  974. /* When waiting for high frequency requests, e.g. during synchronous
  975. * rendering split between the CPU and GPU, the finite amount of time
  976. * required to set up the irq and wait upon it limits the response
  977. * rate. By busywaiting on the request completion for a short while we
  978. * can service the high frequency waits as quick as possible. However,
  979. * if it is a slow request, we want to sleep as quickly as possible.
  980. * The tradeoff between waiting and sleeping is roughly the time it
  981. * takes to sleep on a request, on the order of a microsecond.
  982. */
  983. if (req->engine->irq_refcount)
  984. return -EBUSY;
  985. /* Only spin if we know the GPU is processing this request */
  986. if (!i915_gem_request_started(req, true))
  987. return -EAGAIN;
  988. timeout = local_clock_us(&cpu) + 5;
  989. while (!need_resched()) {
  990. if (i915_gem_request_completed(req, true))
  991. return 0;
  992. if (signal_pending_state(state, current))
  993. break;
  994. if (busywait_stop(timeout, cpu))
  995. break;
  996. cpu_relax_lowlatency();
  997. }
  998. if (i915_gem_request_completed(req, false))
  999. return 0;
  1000. return -EAGAIN;
  1001. }
  1002. /**
  1003. * __i915_wait_request - wait until execution of request has finished
  1004. * @req: duh!
  1005. * @interruptible: do an interruptible wait (normally yes)
  1006. * @timeout: in - how long to wait (NULL forever); out - how much time remaining
  1007. *
  1008. * Note: It is of utmost importance that the passed in seqno and reset_counter
  1009. * values have been read by the caller in an smp safe manner. Where read-side
  1010. * locks are involved, it is sufficient to read the reset_counter before
  1011. * unlocking the lock that protects the seqno. For lockless tricks, the
  1012. * reset_counter _must_ be read before, and an appropriate smp_rmb must be
  1013. * inserted.
  1014. *
  1015. * Returns 0 if the request was found within the alloted time. Else returns the
  1016. * errno with remaining time filled in timeout argument.
  1017. */
  1018. int __i915_wait_request(struct drm_i915_gem_request *req,
  1019. bool interruptible,
  1020. s64 *timeout,
  1021. struct intel_rps_client *rps)
  1022. {
  1023. struct intel_engine_cs *engine = i915_gem_request_get_engine(req);
  1024. struct drm_i915_private *dev_priv = req->i915;
  1025. const bool irq_test_in_progress =
  1026. ACCESS_ONCE(dev_priv->gpu_error.test_irq_rings) & intel_engine_flag(engine);
  1027. int state = interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
  1028. DEFINE_WAIT(wait);
  1029. unsigned long timeout_expire;
  1030. s64 before = 0; /* Only to silence a compiler warning. */
  1031. int ret;
  1032. WARN(!intel_irqs_enabled(dev_priv), "IRQs disabled");
  1033. if (list_empty(&req->list))
  1034. return 0;
  1035. if (i915_gem_request_completed(req, true))
  1036. return 0;
  1037. timeout_expire = 0;
  1038. if (timeout) {
  1039. if (WARN_ON(*timeout < 0))
  1040. return -EINVAL;
  1041. if (*timeout == 0)
  1042. return -ETIME;
  1043. timeout_expire = jiffies + nsecs_to_jiffies_timeout(*timeout);
  1044. /*
  1045. * Record current time in case interrupted by signal, or wedged.
  1046. */
  1047. before = ktime_get_raw_ns();
  1048. }
  1049. if (INTEL_INFO(dev_priv)->gen >= 6)
  1050. gen6_rps_boost(dev_priv, rps, req->emitted_jiffies);
  1051. trace_i915_gem_request_wait_begin(req);
  1052. /* Optimistic spin for the next jiffie before touching IRQs */
  1053. ret = __i915_spin_request(req, state);
  1054. if (ret == 0)
  1055. goto out;
  1056. if (!irq_test_in_progress && WARN_ON(!engine->irq_get(engine))) {
  1057. ret = -ENODEV;
  1058. goto out;
  1059. }
  1060. for (;;) {
  1061. struct timer_list timer;
  1062. prepare_to_wait(&engine->irq_queue, &wait, state);
  1063. /* We need to check whether any gpu reset happened in between
  1064. * the request being submitted and now. If a reset has occurred,
  1065. * the request is effectively complete (we either are in the
  1066. * process of or have discarded the rendering and completely
  1067. * reset the GPU. The results of the request are lost and we
  1068. * are free to continue on with the original operation.
  1069. */
  1070. if (req->reset_counter != i915_reset_counter(&dev_priv->gpu_error)) {
  1071. ret = 0;
  1072. break;
  1073. }
  1074. if (i915_gem_request_completed(req, false)) {
  1075. ret = 0;
  1076. break;
  1077. }
  1078. if (signal_pending_state(state, current)) {
  1079. ret = -ERESTARTSYS;
  1080. break;
  1081. }
  1082. if (timeout && time_after_eq(jiffies, timeout_expire)) {
  1083. ret = -ETIME;
  1084. break;
  1085. }
  1086. timer.function = NULL;
  1087. if (timeout || missed_irq(dev_priv, engine)) {
  1088. unsigned long expire;
  1089. setup_timer_on_stack(&timer, fake_irq, (unsigned long)current);
  1090. expire = missed_irq(dev_priv, engine) ? jiffies + 1 : timeout_expire;
  1091. mod_timer(&timer, expire);
  1092. }
  1093. io_schedule();
  1094. if (timer.function) {
  1095. del_singleshot_timer_sync(&timer);
  1096. destroy_timer_on_stack(&timer);
  1097. }
  1098. }
  1099. if (!irq_test_in_progress)
  1100. engine->irq_put(engine);
  1101. finish_wait(&engine->irq_queue, &wait);
  1102. out:
  1103. trace_i915_gem_request_wait_end(req);
  1104. if (timeout) {
  1105. s64 tres = *timeout - (ktime_get_raw_ns() - before);
  1106. *timeout = tres < 0 ? 0 : tres;
  1107. /*
  1108. * Apparently ktime isn't accurate enough and occasionally has a
  1109. * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
  1110. * things up to make the test happy. We allow up to 1 jiffy.
  1111. *
  1112. * This is a regrssion from the timespec->ktime conversion.
  1113. */
  1114. if (ret == -ETIME && *timeout < jiffies_to_usecs(1)*1000)
  1115. *timeout = 0;
  1116. }
  1117. return ret;
  1118. }
  1119. int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
  1120. struct drm_file *file)
  1121. {
  1122. struct drm_i915_file_private *file_priv;
  1123. WARN_ON(!req || !file || req->file_priv);
  1124. if (!req || !file)
  1125. return -EINVAL;
  1126. if (req->file_priv)
  1127. return -EINVAL;
  1128. file_priv = file->driver_priv;
  1129. spin_lock(&file_priv->mm.lock);
  1130. req->file_priv = file_priv;
  1131. list_add_tail(&req->client_list, &file_priv->mm.request_list);
  1132. spin_unlock(&file_priv->mm.lock);
  1133. req->pid = get_pid(task_pid(current));
  1134. return 0;
  1135. }
  1136. static inline void
  1137. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1138. {
  1139. struct drm_i915_file_private *file_priv = request->file_priv;
  1140. if (!file_priv)
  1141. return;
  1142. spin_lock(&file_priv->mm.lock);
  1143. list_del(&request->client_list);
  1144. request->file_priv = NULL;
  1145. spin_unlock(&file_priv->mm.lock);
  1146. put_pid(request->pid);
  1147. request->pid = NULL;
  1148. }
  1149. static void i915_gem_request_retire(struct drm_i915_gem_request *request)
  1150. {
  1151. trace_i915_gem_request_retire(request);
  1152. /* We know the GPU must have read the request to have
  1153. * sent us the seqno + interrupt, so use the position
  1154. * of tail of the request to update the last known position
  1155. * of the GPU head.
  1156. *
  1157. * Note this requires that we are always called in request
  1158. * completion order.
  1159. */
  1160. request->ringbuf->last_retired_head = request->postfix;
  1161. list_del_init(&request->list);
  1162. i915_gem_request_remove_from_client(request);
  1163. if (request->previous_context) {
  1164. if (i915.enable_execlists)
  1165. intel_lr_context_unpin(request->previous_context,
  1166. request->engine);
  1167. }
  1168. i915_gem_context_unreference(request->ctx);
  1169. i915_gem_request_unreference(request);
  1170. }
  1171. static void
  1172. __i915_gem_request_retire__upto(struct drm_i915_gem_request *req)
  1173. {
  1174. struct intel_engine_cs *engine = req->engine;
  1175. struct drm_i915_gem_request *tmp;
  1176. lockdep_assert_held(&engine->i915->dev->struct_mutex);
  1177. if (list_empty(&req->list))
  1178. return;
  1179. do {
  1180. tmp = list_first_entry(&engine->request_list,
  1181. typeof(*tmp), list);
  1182. i915_gem_request_retire(tmp);
  1183. } while (tmp != req);
  1184. WARN_ON(i915_verify_lists(engine->dev));
  1185. }
  1186. /**
  1187. * Waits for a request to be signaled, and cleans up the
  1188. * request and object lists appropriately for that event.
  1189. */
  1190. int
  1191. i915_wait_request(struct drm_i915_gem_request *req)
  1192. {
  1193. struct drm_i915_private *dev_priv = req->i915;
  1194. bool interruptible;
  1195. int ret;
  1196. interruptible = dev_priv->mm.interruptible;
  1197. BUG_ON(!mutex_is_locked(&dev_priv->dev->struct_mutex));
  1198. ret = __i915_wait_request(req, interruptible, NULL, NULL);
  1199. if (ret)
  1200. return ret;
  1201. /* If the GPU hung, we want to keep the requests to find the guilty. */
  1202. if (req->reset_counter == i915_reset_counter(&dev_priv->gpu_error))
  1203. __i915_gem_request_retire__upto(req);
  1204. return 0;
  1205. }
  1206. /**
  1207. * Ensures that all rendering to the object has completed and the object is
  1208. * safe to unbind from the GTT or access from the CPU.
  1209. */
  1210. int
  1211. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  1212. bool readonly)
  1213. {
  1214. int ret, i;
  1215. if (!obj->active)
  1216. return 0;
  1217. if (readonly) {
  1218. if (obj->last_write_req != NULL) {
  1219. ret = i915_wait_request(obj->last_write_req);
  1220. if (ret)
  1221. return ret;
  1222. i = obj->last_write_req->engine->id;
  1223. if (obj->last_read_req[i] == obj->last_write_req)
  1224. i915_gem_object_retire__read(obj, i);
  1225. else
  1226. i915_gem_object_retire__write(obj);
  1227. }
  1228. } else {
  1229. for (i = 0; i < I915_NUM_ENGINES; i++) {
  1230. if (obj->last_read_req[i] == NULL)
  1231. continue;
  1232. ret = i915_wait_request(obj->last_read_req[i]);
  1233. if (ret)
  1234. return ret;
  1235. i915_gem_object_retire__read(obj, i);
  1236. }
  1237. GEM_BUG_ON(obj->active);
  1238. }
  1239. return 0;
  1240. }
  1241. static void
  1242. i915_gem_object_retire_request(struct drm_i915_gem_object *obj,
  1243. struct drm_i915_gem_request *req)
  1244. {
  1245. int ring = req->engine->id;
  1246. if (obj->last_read_req[ring] == req)
  1247. i915_gem_object_retire__read(obj, ring);
  1248. else if (obj->last_write_req == req)
  1249. i915_gem_object_retire__write(obj);
  1250. if (req->reset_counter == i915_reset_counter(&req->i915->gpu_error))
  1251. __i915_gem_request_retire__upto(req);
  1252. }
  1253. /* A nonblocking variant of the above wait. This is a highly dangerous routine
  1254. * as the object state may change during this call.
  1255. */
  1256. static __must_check int
  1257. i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
  1258. struct intel_rps_client *rps,
  1259. bool readonly)
  1260. {
  1261. struct drm_device *dev = obj->base.dev;
  1262. struct drm_i915_private *dev_priv = dev->dev_private;
  1263. struct drm_i915_gem_request *requests[I915_NUM_ENGINES];
  1264. int ret, i, n = 0;
  1265. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1266. BUG_ON(!dev_priv->mm.interruptible);
  1267. if (!obj->active)
  1268. return 0;
  1269. if (readonly) {
  1270. struct drm_i915_gem_request *req;
  1271. req = obj->last_write_req;
  1272. if (req == NULL)
  1273. return 0;
  1274. requests[n++] = i915_gem_request_reference(req);
  1275. } else {
  1276. for (i = 0; i < I915_NUM_ENGINES; i++) {
  1277. struct drm_i915_gem_request *req;
  1278. req = obj->last_read_req[i];
  1279. if (req == NULL)
  1280. continue;
  1281. requests[n++] = i915_gem_request_reference(req);
  1282. }
  1283. }
  1284. mutex_unlock(&dev->struct_mutex);
  1285. ret = 0;
  1286. for (i = 0; ret == 0 && i < n; i++)
  1287. ret = __i915_wait_request(requests[i], true, NULL, rps);
  1288. mutex_lock(&dev->struct_mutex);
  1289. for (i = 0; i < n; i++) {
  1290. if (ret == 0)
  1291. i915_gem_object_retire_request(obj, requests[i]);
  1292. i915_gem_request_unreference(requests[i]);
  1293. }
  1294. return ret;
  1295. }
  1296. static struct intel_rps_client *to_rps_client(struct drm_file *file)
  1297. {
  1298. struct drm_i915_file_private *fpriv = file->driver_priv;
  1299. return &fpriv->rps;
  1300. }
  1301. /**
  1302. * Called when user space prepares to use an object with the CPU, either
  1303. * through the mmap ioctl's mapping or a GTT mapping.
  1304. */
  1305. int
  1306. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  1307. struct drm_file *file)
  1308. {
  1309. struct drm_i915_gem_set_domain *args = data;
  1310. struct drm_i915_gem_object *obj;
  1311. uint32_t read_domains = args->read_domains;
  1312. uint32_t write_domain = args->write_domain;
  1313. int ret;
  1314. /* Only handle setting domains to types used by the CPU. */
  1315. if (write_domain & I915_GEM_GPU_DOMAINS)
  1316. return -EINVAL;
  1317. if (read_domains & I915_GEM_GPU_DOMAINS)
  1318. return -EINVAL;
  1319. /* Having something in the write domain implies it's in the read
  1320. * domain, and only that read domain. Enforce that in the request.
  1321. */
  1322. if (write_domain != 0 && read_domains != write_domain)
  1323. return -EINVAL;
  1324. ret = i915_mutex_lock_interruptible(dev);
  1325. if (ret)
  1326. return ret;
  1327. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  1328. if (&obj->base == NULL) {
  1329. ret = -ENOENT;
  1330. goto unlock;
  1331. }
  1332. /* Try to flush the object off the GPU without holding the lock.
  1333. * We will repeat the flush holding the lock in the normal manner
  1334. * to catch cases where we are gazumped.
  1335. */
  1336. ret = i915_gem_object_wait_rendering__nonblocking(obj,
  1337. to_rps_client(file),
  1338. !write_domain);
  1339. if (ret)
  1340. goto unref;
  1341. if (read_domains & I915_GEM_DOMAIN_GTT)
  1342. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1343. else
  1344. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1345. if (write_domain != 0)
  1346. intel_fb_obj_invalidate(obj,
  1347. write_domain == I915_GEM_DOMAIN_GTT ?
  1348. ORIGIN_GTT : ORIGIN_CPU);
  1349. unref:
  1350. drm_gem_object_unreference(&obj->base);
  1351. unlock:
  1352. mutex_unlock(&dev->struct_mutex);
  1353. return ret;
  1354. }
  1355. /**
  1356. * Called when user space has done writes to this buffer
  1357. */
  1358. int
  1359. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1360. struct drm_file *file)
  1361. {
  1362. struct drm_i915_gem_sw_finish *args = data;
  1363. struct drm_i915_gem_object *obj;
  1364. int ret = 0;
  1365. ret = i915_mutex_lock_interruptible(dev);
  1366. if (ret)
  1367. return ret;
  1368. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  1369. if (&obj->base == NULL) {
  1370. ret = -ENOENT;
  1371. goto unlock;
  1372. }
  1373. /* Pinned buffers may be scanout, so flush the cache */
  1374. if (obj->pin_display)
  1375. i915_gem_object_flush_cpu_write_domain(obj);
  1376. drm_gem_object_unreference(&obj->base);
  1377. unlock:
  1378. mutex_unlock(&dev->struct_mutex);
  1379. return ret;
  1380. }
  1381. /**
  1382. * Maps the contents of an object, returning the address it is mapped
  1383. * into.
  1384. *
  1385. * While the mapping holds a reference on the contents of the object, it doesn't
  1386. * imply a ref on the object itself.
  1387. *
  1388. * IMPORTANT:
  1389. *
  1390. * DRM driver writers who look a this function as an example for how to do GEM
  1391. * mmap support, please don't implement mmap support like here. The modern way
  1392. * to implement DRM mmap support is with an mmap offset ioctl (like
  1393. * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
  1394. * That way debug tooling like valgrind will understand what's going on, hiding
  1395. * the mmap call in a driver private ioctl will break that. The i915 driver only
  1396. * does cpu mmaps this way because we didn't know better.
  1397. */
  1398. int
  1399. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1400. struct drm_file *file)
  1401. {
  1402. struct drm_i915_gem_mmap *args = data;
  1403. struct drm_gem_object *obj;
  1404. unsigned long addr;
  1405. if (args->flags & ~(I915_MMAP_WC))
  1406. return -EINVAL;
  1407. if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
  1408. return -ENODEV;
  1409. obj = drm_gem_object_lookup(file, args->handle);
  1410. if (obj == NULL)
  1411. return -ENOENT;
  1412. /* prime objects have no backing filp to GEM mmap
  1413. * pages from.
  1414. */
  1415. if (!obj->filp) {
  1416. drm_gem_object_unreference_unlocked(obj);
  1417. return -EINVAL;
  1418. }
  1419. addr = vm_mmap(obj->filp, 0, args->size,
  1420. PROT_READ | PROT_WRITE, MAP_SHARED,
  1421. args->offset);
  1422. if (args->flags & I915_MMAP_WC) {
  1423. struct mm_struct *mm = current->mm;
  1424. struct vm_area_struct *vma;
  1425. if (down_write_killable(&mm->mmap_sem)) {
  1426. drm_gem_object_unreference_unlocked(obj);
  1427. return -EINTR;
  1428. }
  1429. vma = find_vma(mm, addr);
  1430. if (vma)
  1431. vma->vm_page_prot =
  1432. pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  1433. else
  1434. addr = -ENOMEM;
  1435. up_write(&mm->mmap_sem);
  1436. }
  1437. drm_gem_object_unreference_unlocked(obj);
  1438. if (IS_ERR((void *)addr))
  1439. return addr;
  1440. args->addr_ptr = (uint64_t) addr;
  1441. return 0;
  1442. }
  1443. /**
  1444. * i915_gem_fault - fault a page into the GTT
  1445. * @vma: VMA in question
  1446. * @vmf: fault info
  1447. *
  1448. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1449. * from userspace. The fault handler takes care of binding the object to
  1450. * the GTT (if needed), allocating and programming a fence register (again,
  1451. * only if needed based on whether the old reg is still valid or the object
  1452. * is tiled) and inserting a new PTE into the faulting process.
  1453. *
  1454. * Note that the faulting process may involve evicting existing objects
  1455. * from the GTT and/or fence registers to make room. So performance may
  1456. * suffer if the GTT working set is large or there are few fence registers
  1457. * left.
  1458. */
  1459. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1460. {
  1461. struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
  1462. struct drm_device *dev = obj->base.dev;
  1463. struct drm_i915_private *dev_priv = to_i915(dev);
  1464. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1465. struct i915_ggtt_view view = i915_ggtt_view_normal;
  1466. pgoff_t page_offset;
  1467. unsigned long pfn;
  1468. int ret = 0;
  1469. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1470. intel_runtime_pm_get(dev_priv);
  1471. /* We don't use vmf->pgoff since that has the fake offset */
  1472. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  1473. PAGE_SHIFT;
  1474. ret = i915_mutex_lock_interruptible(dev);
  1475. if (ret)
  1476. goto out;
  1477. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1478. /* Try to flush the object off the GPU first without holding the lock.
  1479. * Upon reacquiring the lock, we will perform our sanity checks and then
  1480. * repeat the flush holding the lock in the normal manner to catch cases
  1481. * where we are gazumped.
  1482. */
  1483. ret = i915_gem_object_wait_rendering__nonblocking(obj, NULL, !write);
  1484. if (ret)
  1485. goto unlock;
  1486. /* Access to snoopable pages through the GTT is incoherent. */
  1487. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
  1488. ret = -EFAULT;
  1489. goto unlock;
  1490. }
  1491. /* Use a partial view if the object is bigger than the aperture. */
  1492. if (obj->base.size >= ggtt->mappable_end &&
  1493. obj->tiling_mode == I915_TILING_NONE) {
  1494. static const unsigned int chunk_size = 256; // 1 MiB
  1495. memset(&view, 0, sizeof(view));
  1496. view.type = I915_GGTT_VIEW_PARTIAL;
  1497. view.params.partial.offset = rounddown(page_offset, chunk_size);
  1498. view.params.partial.size =
  1499. min_t(unsigned int,
  1500. chunk_size,
  1501. (vma->vm_end - vma->vm_start)/PAGE_SIZE -
  1502. view.params.partial.offset);
  1503. }
  1504. /* Now pin it into the GTT if needed */
  1505. ret = i915_gem_object_ggtt_pin(obj, &view, 0, PIN_MAPPABLE);
  1506. if (ret)
  1507. goto unlock;
  1508. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1509. if (ret)
  1510. goto unpin;
  1511. ret = i915_gem_object_get_fence(obj);
  1512. if (ret)
  1513. goto unpin;
  1514. /* Finally, remap it using the new GTT offset */
  1515. pfn = ggtt->mappable_base +
  1516. i915_gem_obj_ggtt_offset_view(obj, &view);
  1517. pfn >>= PAGE_SHIFT;
  1518. if (unlikely(view.type == I915_GGTT_VIEW_PARTIAL)) {
  1519. /* Overriding existing pages in partial view does not cause
  1520. * us any trouble as TLBs are still valid because the fault
  1521. * is due to userspace losing part of the mapping or never
  1522. * having accessed it before (at this partials' range).
  1523. */
  1524. unsigned long base = vma->vm_start +
  1525. (view.params.partial.offset << PAGE_SHIFT);
  1526. unsigned int i;
  1527. for (i = 0; i < view.params.partial.size; i++) {
  1528. ret = vm_insert_pfn(vma, base + i * PAGE_SIZE, pfn + i);
  1529. if (ret)
  1530. break;
  1531. }
  1532. obj->fault_mappable = true;
  1533. } else {
  1534. if (!obj->fault_mappable) {
  1535. unsigned long size = min_t(unsigned long,
  1536. vma->vm_end - vma->vm_start,
  1537. obj->base.size);
  1538. int i;
  1539. for (i = 0; i < size >> PAGE_SHIFT; i++) {
  1540. ret = vm_insert_pfn(vma,
  1541. (unsigned long)vma->vm_start + i * PAGE_SIZE,
  1542. pfn + i);
  1543. if (ret)
  1544. break;
  1545. }
  1546. obj->fault_mappable = true;
  1547. } else
  1548. ret = vm_insert_pfn(vma,
  1549. (unsigned long)vmf->virtual_address,
  1550. pfn + page_offset);
  1551. }
  1552. unpin:
  1553. i915_gem_object_ggtt_unpin_view(obj, &view);
  1554. unlock:
  1555. mutex_unlock(&dev->struct_mutex);
  1556. out:
  1557. switch (ret) {
  1558. case -EIO:
  1559. /*
  1560. * We eat errors when the gpu is terminally wedged to avoid
  1561. * userspace unduly crashing (gl has no provisions for mmaps to
  1562. * fail). But any other -EIO isn't ours (e.g. swap in failure)
  1563. * and so needs to be reported.
  1564. */
  1565. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  1566. ret = VM_FAULT_SIGBUS;
  1567. break;
  1568. }
  1569. case -EAGAIN:
  1570. /*
  1571. * EAGAIN means the gpu is hung and we'll wait for the error
  1572. * handler to reset everything when re-faulting in
  1573. * i915_mutex_lock_interruptible.
  1574. */
  1575. case 0:
  1576. case -ERESTARTSYS:
  1577. case -EINTR:
  1578. case -EBUSY:
  1579. /*
  1580. * EBUSY is ok: this just means that another thread
  1581. * already did the job.
  1582. */
  1583. ret = VM_FAULT_NOPAGE;
  1584. break;
  1585. case -ENOMEM:
  1586. ret = VM_FAULT_OOM;
  1587. break;
  1588. case -ENOSPC:
  1589. case -EFAULT:
  1590. ret = VM_FAULT_SIGBUS;
  1591. break;
  1592. default:
  1593. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1594. ret = VM_FAULT_SIGBUS;
  1595. break;
  1596. }
  1597. intel_runtime_pm_put(dev_priv);
  1598. return ret;
  1599. }
  1600. /**
  1601. * i915_gem_release_mmap - remove physical page mappings
  1602. * @obj: obj in question
  1603. *
  1604. * Preserve the reservation of the mmapping with the DRM core code, but
  1605. * relinquish ownership of the pages back to the system.
  1606. *
  1607. * It is vital that we remove the page mapping if we have mapped a tiled
  1608. * object through the GTT and then lose the fence register due to
  1609. * resource pressure. Similarly if the object has been moved out of the
  1610. * aperture, than pages mapped into userspace must be revoked. Removing the
  1611. * mapping will then trigger a page fault on the next user access, allowing
  1612. * fixup by i915_gem_fault().
  1613. */
  1614. void
  1615. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1616. {
  1617. /* Serialisation between user GTT access and our code depends upon
  1618. * revoking the CPU's PTE whilst the mutex is held. The next user
  1619. * pagefault then has to wait until we release the mutex.
  1620. */
  1621. lockdep_assert_held(&obj->base.dev->struct_mutex);
  1622. if (!obj->fault_mappable)
  1623. return;
  1624. drm_vma_node_unmap(&obj->base.vma_node,
  1625. obj->base.dev->anon_inode->i_mapping);
  1626. /* Ensure that the CPU's PTE are revoked and there are not outstanding
  1627. * memory transactions from userspace before we return. The TLB
  1628. * flushing implied above by changing the PTE above *should* be
  1629. * sufficient, an extra barrier here just provides us with a bit
  1630. * of paranoid documentation about our requirement to serialise
  1631. * memory writes before touching registers / GSM.
  1632. */
  1633. wmb();
  1634. obj->fault_mappable = false;
  1635. }
  1636. void
  1637. i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv)
  1638. {
  1639. struct drm_i915_gem_object *obj;
  1640. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  1641. i915_gem_release_mmap(obj);
  1642. }
  1643. uint32_t
  1644. i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
  1645. {
  1646. uint32_t gtt_size;
  1647. if (INTEL_INFO(dev)->gen >= 4 ||
  1648. tiling_mode == I915_TILING_NONE)
  1649. return size;
  1650. /* Previous chips need a power-of-two fence region when tiling */
  1651. if (IS_GEN3(dev))
  1652. gtt_size = 1024*1024;
  1653. else
  1654. gtt_size = 512*1024;
  1655. while (gtt_size < size)
  1656. gtt_size <<= 1;
  1657. return gtt_size;
  1658. }
  1659. /**
  1660. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1661. * @obj: object to check
  1662. *
  1663. * Return the required GTT alignment for an object, taking into account
  1664. * potential fence register mapping.
  1665. */
  1666. uint32_t
  1667. i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
  1668. int tiling_mode, bool fenced)
  1669. {
  1670. /*
  1671. * Minimum alignment is 4k (GTT page size), but might be greater
  1672. * if a fence register is needed for the object.
  1673. */
  1674. if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
  1675. tiling_mode == I915_TILING_NONE)
  1676. return 4096;
  1677. /*
  1678. * Previous chips need to be aligned to the size of the smallest
  1679. * fence register that can contain the object.
  1680. */
  1681. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1682. }
  1683. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1684. {
  1685. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1686. int ret;
  1687. dev_priv->mm.shrinker_no_lock_stealing = true;
  1688. ret = drm_gem_create_mmap_offset(&obj->base);
  1689. if (ret != -ENOSPC)
  1690. goto out;
  1691. /* Badly fragmented mmap space? The only way we can recover
  1692. * space is by destroying unwanted objects. We can't randomly release
  1693. * mmap_offsets as userspace expects them to be persistent for the
  1694. * lifetime of the objects. The closest we can is to release the
  1695. * offsets on purgeable objects by truncating it and marking it purged,
  1696. * which prevents userspace from ever using that object again.
  1697. */
  1698. i915_gem_shrink(dev_priv,
  1699. obj->base.size >> PAGE_SHIFT,
  1700. I915_SHRINK_BOUND |
  1701. I915_SHRINK_UNBOUND |
  1702. I915_SHRINK_PURGEABLE);
  1703. ret = drm_gem_create_mmap_offset(&obj->base);
  1704. if (ret != -ENOSPC)
  1705. goto out;
  1706. i915_gem_shrink_all(dev_priv);
  1707. ret = drm_gem_create_mmap_offset(&obj->base);
  1708. out:
  1709. dev_priv->mm.shrinker_no_lock_stealing = false;
  1710. return ret;
  1711. }
  1712. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1713. {
  1714. drm_gem_free_mmap_offset(&obj->base);
  1715. }
  1716. int
  1717. i915_gem_mmap_gtt(struct drm_file *file,
  1718. struct drm_device *dev,
  1719. uint32_t handle,
  1720. uint64_t *offset)
  1721. {
  1722. struct drm_i915_gem_object *obj;
  1723. int ret;
  1724. ret = i915_mutex_lock_interruptible(dev);
  1725. if (ret)
  1726. return ret;
  1727. obj = to_intel_bo(drm_gem_object_lookup(file, handle));
  1728. if (&obj->base == NULL) {
  1729. ret = -ENOENT;
  1730. goto unlock;
  1731. }
  1732. if (obj->madv != I915_MADV_WILLNEED) {
  1733. DRM_DEBUG("Attempting to mmap a purgeable buffer\n");
  1734. ret = -EFAULT;
  1735. goto out;
  1736. }
  1737. ret = i915_gem_object_create_mmap_offset(obj);
  1738. if (ret)
  1739. goto out;
  1740. *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  1741. out:
  1742. drm_gem_object_unreference(&obj->base);
  1743. unlock:
  1744. mutex_unlock(&dev->struct_mutex);
  1745. return ret;
  1746. }
  1747. /**
  1748. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1749. * @dev: DRM device
  1750. * @data: GTT mapping ioctl data
  1751. * @file: GEM object info
  1752. *
  1753. * Simply returns the fake offset to userspace so it can mmap it.
  1754. * The mmap call will end up in drm_gem_mmap(), which will set things
  1755. * up so we can get faults in the handler above.
  1756. *
  1757. * The fault handler will take care of binding the object into the GTT
  1758. * (since it may have been evicted to make room for something), allocating
  1759. * a fence register, and mapping the appropriate aperture address into
  1760. * userspace.
  1761. */
  1762. int
  1763. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1764. struct drm_file *file)
  1765. {
  1766. struct drm_i915_gem_mmap_gtt *args = data;
  1767. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1768. }
  1769. /* Immediately discard the backing storage */
  1770. static void
  1771. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1772. {
  1773. i915_gem_object_free_mmap_offset(obj);
  1774. if (obj->base.filp == NULL)
  1775. return;
  1776. /* Our goal here is to return as much of the memory as
  1777. * is possible back to the system as we are called from OOM.
  1778. * To do this we must instruct the shmfs to drop all of its
  1779. * backing pages, *now*.
  1780. */
  1781. shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
  1782. obj->madv = __I915_MADV_PURGED;
  1783. }
  1784. /* Try to discard unwanted pages */
  1785. static void
  1786. i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
  1787. {
  1788. struct address_space *mapping;
  1789. switch (obj->madv) {
  1790. case I915_MADV_DONTNEED:
  1791. i915_gem_object_truncate(obj);
  1792. case __I915_MADV_PURGED:
  1793. return;
  1794. }
  1795. if (obj->base.filp == NULL)
  1796. return;
  1797. mapping = file_inode(obj->base.filp)->i_mapping,
  1798. invalidate_mapping_pages(mapping, 0, (loff_t)-1);
  1799. }
  1800. static void
  1801. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1802. {
  1803. struct sgt_iter sgt_iter;
  1804. struct page *page;
  1805. int ret;
  1806. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1807. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  1808. if (WARN_ON(ret)) {
  1809. /* In the event of a disaster, abandon all caches and
  1810. * hope for the best.
  1811. */
  1812. i915_gem_clflush_object(obj, true);
  1813. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1814. }
  1815. i915_gem_gtt_finish_object(obj);
  1816. if (i915_gem_object_needs_bit17_swizzle(obj))
  1817. i915_gem_object_save_bit_17_swizzle(obj);
  1818. if (obj->madv == I915_MADV_DONTNEED)
  1819. obj->dirty = 0;
  1820. for_each_sgt_page(page, sgt_iter, obj->pages) {
  1821. if (obj->dirty)
  1822. set_page_dirty(page);
  1823. if (obj->madv == I915_MADV_WILLNEED)
  1824. mark_page_accessed(page);
  1825. put_page(page);
  1826. }
  1827. obj->dirty = 0;
  1828. sg_free_table(obj->pages);
  1829. kfree(obj->pages);
  1830. }
  1831. int
  1832. i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
  1833. {
  1834. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1835. if (obj->pages == NULL)
  1836. return 0;
  1837. if (obj->pages_pin_count)
  1838. return -EBUSY;
  1839. BUG_ON(i915_gem_obj_bound_any(obj));
  1840. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1841. * array, hence protect them from being reaped by removing them from gtt
  1842. * lists early. */
  1843. list_del(&obj->global_list);
  1844. if (obj->mapping) {
  1845. if (is_vmalloc_addr(obj->mapping))
  1846. vunmap(obj->mapping);
  1847. else
  1848. kunmap(kmap_to_page(obj->mapping));
  1849. obj->mapping = NULL;
  1850. }
  1851. ops->put_pages(obj);
  1852. obj->pages = NULL;
  1853. i915_gem_object_invalidate(obj);
  1854. return 0;
  1855. }
  1856. static int
  1857. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1858. {
  1859. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1860. int page_count, i;
  1861. struct address_space *mapping;
  1862. struct sg_table *st;
  1863. struct scatterlist *sg;
  1864. struct sgt_iter sgt_iter;
  1865. struct page *page;
  1866. unsigned long last_pfn = 0; /* suppress gcc warning */
  1867. int ret;
  1868. gfp_t gfp;
  1869. /* Assert that the object is not currently in any GPU domain. As it
  1870. * wasn't in the GTT, there shouldn't be any way it could have been in
  1871. * a GPU cache
  1872. */
  1873. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1874. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1875. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1876. if (st == NULL)
  1877. return -ENOMEM;
  1878. page_count = obj->base.size / PAGE_SIZE;
  1879. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1880. kfree(st);
  1881. return -ENOMEM;
  1882. }
  1883. /* Get the list of pages out of our struct file. They'll be pinned
  1884. * at this point until we release them.
  1885. *
  1886. * Fail silently without starting the shrinker
  1887. */
  1888. mapping = file_inode(obj->base.filp)->i_mapping;
  1889. gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
  1890. gfp |= __GFP_NORETRY | __GFP_NOWARN;
  1891. sg = st->sgl;
  1892. st->nents = 0;
  1893. for (i = 0; i < page_count; i++) {
  1894. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1895. if (IS_ERR(page)) {
  1896. i915_gem_shrink(dev_priv,
  1897. page_count,
  1898. I915_SHRINK_BOUND |
  1899. I915_SHRINK_UNBOUND |
  1900. I915_SHRINK_PURGEABLE);
  1901. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1902. }
  1903. if (IS_ERR(page)) {
  1904. /* We've tried hard to allocate the memory by reaping
  1905. * our own buffer, now let the real VM do its job and
  1906. * go down in flames if truly OOM.
  1907. */
  1908. i915_gem_shrink_all(dev_priv);
  1909. page = shmem_read_mapping_page(mapping, i);
  1910. if (IS_ERR(page)) {
  1911. ret = PTR_ERR(page);
  1912. goto err_pages;
  1913. }
  1914. }
  1915. #ifdef CONFIG_SWIOTLB
  1916. if (swiotlb_nr_tbl()) {
  1917. st->nents++;
  1918. sg_set_page(sg, page, PAGE_SIZE, 0);
  1919. sg = sg_next(sg);
  1920. continue;
  1921. }
  1922. #endif
  1923. if (!i || page_to_pfn(page) != last_pfn + 1) {
  1924. if (i)
  1925. sg = sg_next(sg);
  1926. st->nents++;
  1927. sg_set_page(sg, page, PAGE_SIZE, 0);
  1928. } else {
  1929. sg->length += PAGE_SIZE;
  1930. }
  1931. last_pfn = page_to_pfn(page);
  1932. /* Check that the i965g/gm workaround works. */
  1933. WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
  1934. }
  1935. #ifdef CONFIG_SWIOTLB
  1936. if (!swiotlb_nr_tbl())
  1937. #endif
  1938. sg_mark_end(sg);
  1939. obj->pages = st;
  1940. ret = i915_gem_gtt_prepare_object(obj);
  1941. if (ret)
  1942. goto err_pages;
  1943. if (i915_gem_object_needs_bit17_swizzle(obj))
  1944. i915_gem_object_do_bit_17_swizzle(obj);
  1945. if (obj->tiling_mode != I915_TILING_NONE &&
  1946. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
  1947. i915_gem_object_pin_pages(obj);
  1948. return 0;
  1949. err_pages:
  1950. sg_mark_end(sg);
  1951. for_each_sgt_page(page, sgt_iter, st)
  1952. put_page(page);
  1953. sg_free_table(st);
  1954. kfree(st);
  1955. /* shmemfs first checks if there is enough memory to allocate the page
  1956. * and reports ENOSPC should there be insufficient, along with the usual
  1957. * ENOMEM for a genuine allocation failure.
  1958. *
  1959. * We use ENOSPC in our driver to mean that we have run out of aperture
  1960. * space and so want to translate the error from shmemfs back to our
  1961. * usual understanding of ENOMEM.
  1962. */
  1963. if (ret == -ENOSPC)
  1964. ret = -ENOMEM;
  1965. return ret;
  1966. }
  1967. /* Ensure that the associated pages are gathered from the backing storage
  1968. * and pinned into our object. i915_gem_object_get_pages() may be called
  1969. * multiple times before they are released by a single call to
  1970. * i915_gem_object_put_pages() - once the pages are no longer referenced
  1971. * either as a result of memory pressure (reaping pages under the shrinker)
  1972. * or as the object is itself released.
  1973. */
  1974. int
  1975. i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  1976. {
  1977. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1978. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1979. int ret;
  1980. if (obj->pages)
  1981. return 0;
  1982. if (obj->madv != I915_MADV_WILLNEED) {
  1983. DRM_DEBUG("Attempting to obtain a purgeable object\n");
  1984. return -EFAULT;
  1985. }
  1986. BUG_ON(obj->pages_pin_count);
  1987. ret = ops->get_pages(obj);
  1988. if (ret)
  1989. return ret;
  1990. list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  1991. obj->get_page.sg = obj->pages->sgl;
  1992. obj->get_page.last = 0;
  1993. return 0;
  1994. }
  1995. /* The 'mapping' part of i915_gem_object_pin_map() below */
  1996. static void *i915_gem_object_map(const struct drm_i915_gem_object *obj)
  1997. {
  1998. unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
  1999. struct sg_table *sgt = obj->pages;
  2000. struct sgt_iter sgt_iter;
  2001. struct page *page;
  2002. struct page *stack_pages[32];
  2003. struct page **pages = stack_pages;
  2004. unsigned long i = 0;
  2005. void *addr;
  2006. /* A single page can always be kmapped */
  2007. if (n_pages == 1)
  2008. return kmap(sg_page(sgt->sgl));
  2009. if (n_pages > ARRAY_SIZE(stack_pages)) {
  2010. /* Too big for stack -- allocate temporary array instead */
  2011. pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY);
  2012. if (!pages)
  2013. return NULL;
  2014. }
  2015. for_each_sgt_page(page, sgt_iter, sgt)
  2016. pages[i++] = page;
  2017. /* Check that we have the expected number of pages */
  2018. GEM_BUG_ON(i != n_pages);
  2019. addr = vmap(pages, n_pages, 0, PAGE_KERNEL);
  2020. if (pages != stack_pages)
  2021. drm_free_large(pages);
  2022. return addr;
  2023. }
  2024. /* get, pin, and map the pages of the object into kernel space */
  2025. void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj)
  2026. {
  2027. int ret;
  2028. lockdep_assert_held(&obj->base.dev->struct_mutex);
  2029. ret = i915_gem_object_get_pages(obj);
  2030. if (ret)
  2031. return ERR_PTR(ret);
  2032. i915_gem_object_pin_pages(obj);
  2033. if (!obj->mapping) {
  2034. obj->mapping = i915_gem_object_map(obj);
  2035. if (!obj->mapping) {
  2036. i915_gem_object_unpin_pages(obj);
  2037. return ERR_PTR(-ENOMEM);
  2038. }
  2039. }
  2040. return obj->mapping;
  2041. }
  2042. void i915_vma_move_to_active(struct i915_vma *vma,
  2043. struct drm_i915_gem_request *req)
  2044. {
  2045. struct drm_i915_gem_object *obj = vma->obj;
  2046. struct intel_engine_cs *engine;
  2047. engine = i915_gem_request_get_engine(req);
  2048. /* Add a reference if we're newly entering the active list. */
  2049. if (obj->active == 0)
  2050. drm_gem_object_reference(&obj->base);
  2051. obj->active |= intel_engine_flag(engine);
  2052. list_move_tail(&obj->engine_list[engine->id], &engine->active_list);
  2053. i915_gem_request_assign(&obj->last_read_req[engine->id], req);
  2054. list_move_tail(&vma->vm_link, &vma->vm->active_list);
  2055. }
  2056. static void
  2057. i915_gem_object_retire__write(struct drm_i915_gem_object *obj)
  2058. {
  2059. GEM_BUG_ON(obj->last_write_req == NULL);
  2060. GEM_BUG_ON(!(obj->active & intel_engine_flag(obj->last_write_req->engine)));
  2061. i915_gem_request_assign(&obj->last_write_req, NULL);
  2062. intel_fb_obj_flush(obj, true, ORIGIN_CS);
  2063. }
  2064. static void
  2065. i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring)
  2066. {
  2067. struct i915_vma *vma;
  2068. GEM_BUG_ON(obj->last_read_req[ring] == NULL);
  2069. GEM_BUG_ON(!(obj->active & (1 << ring)));
  2070. list_del_init(&obj->engine_list[ring]);
  2071. i915_gem_request_assign(&obj->last_read_req[ring], NULL);
  2072. if (obj->last_write_req && obj->last_write_req->engine->id == ring)
  2073. i915_gem_object_retire__write(obj);
  2074. obj->active &= ~(1 << ring);
  2075. if (obj->active)
  2076. return;
  2077. /* Bump our place on the bound list to keep it roughly in LRU order
  2078. * so that we don't steal from recently used but inactive objects
  2079. * (unless we are forced to ofc!)
  2080. */
  2081. list_move_tail(&obj->global_list,
  2082. &to_i915(obj->base.dev)->mm.bound_list);
  2083. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2084. if (!list_empty(&vma->vm_link))
  2085. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  2086. }
  2087. i915_gem_request_assign(&obj->last_fenced_req, NULL);
  2088. drm_gem_object_unreference(&obj->base);
  2089. }
  2090. static int
  2091. i915_gem_init_seqno(struct drm_i915_private *dev_priv, u32 seqno)
  2092. {
  2093. struct intel_engine_cs *engine;
  2094. int ret;
  2095. /* Carefully retire all requests without writing to the rings */
  2096. for_each_engine(engine, dev_priv) {
  2097. ret = intel_engine_idle(engine);
  2098. if (ret)
  2099. return ret;
  2100. }
  2101. i915_gem_retire_requests(dev_priv);
  2102. /* Finally reset hw state */
  2103. for_each_engine(engine, dev_priv)
  2104. intel_ring_init_seqno(engine, seqno);
  2105. return 0;
  2106. }
  2107. int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
  2108. {
  2109. struct drm_i915_private *dev_priv = dev->dev_private;
  2110. int ret;
  2111. if (seqno == 0)
  2112. return -EINVAL;
  2113. /* HWS page needs to be set less than what we
  2114. * will inject to ring
  2115. */
  2116. ret = i915_gem_init_seqno(dev_priv, seqno - 1);
  2117. if (ret)
  2118. return ret;
  2119. /* Carefully set the last_seqno value so that wrap
  2120. * detection still works
  2121. */
  2122. dev_priv->next_seqno = seqno;
  2123. dev_priv->last_seqno = seqno - 1;
  2124. if (dev_priv->last_seqno == 0)
  2125. dev_priv->last_seqno--;
  2126. return 0;
  2127. }
  2128. int
  2129. i915_gem_get_seqno(struct drm_i915_private *dev_priv, u32 *seqno)
  2130. {
  2131. /* reserve 0 for non-seqno */
  2132. if (dev_priv->next_seqno == 0) {
  2133. int ret = i915_gem_init_seqno(dev_priv, 0);
  2134. if (ret)
  2135. return ret;
  2136. dev_priv->next_seqno = 1;
  2137. }
  2138. *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
  2139. return 0;
  2140. }
  2141. /*
  2142. * NB: This function is not allowed to fail. Doing so would mean the the
  2143. * request is not being tracked for completion but the work itself is
  2144. * going to happen on the hardware. This would be a Bad Thing(tm).
  2145. */
  2146. void __i915_add_request(struct drm_i915_gem_request *request,
  2147. struct drm_i915_gem_object *obj,
  2148. bool flush_caches)
  2149. {
  2150. struct intel_engine_cs *engine;
  2151. struct drm_i915_private *dev_priv;
  2152. struct intel_ringbuffer *ringbuf;
  2153. u32 request_start;
  2154. u32 reserved_tail;
  2155. int ret;
  2156. if (WARN_ON(request == NULL))
  2157. return;
  2158. engine = request->engine;
  2159. dev_priv = request->i915;
  2160. ringbuf = request->ringbuf;
  2161. /*
  2162. * To ensure that this call will not fail, space for its emissions
  2163. * should already have been reserved in the ring buffer. Let the ring
  2164. * know that it is time to use that space up.
  2165. */
  2166. request_start = intel_ring_get_tail(ringbuf);
  2167. reserved_tail = request->reserved_space;
  2168. request->reserved_space = 0;
  2169. /*
  2170. * Emit any outstanding flushes - execbuf can fail to emit the flush
  2171. * after having emitted the batchbuffer command. Hence we need to fix
  2172. * things up similar to emitting the lazy request. The difference here
  2173. * is that the flush _must_ happen before the next request, no matter
  2174. * what.
  2175. */
  2176. if (flush_caches) {
  2177. if (i915.enable_execlists)
  2178. ret = logical_ring_flush_all_caches(request);
  2179. else
  2180. ret = intel_ring_flush_all_caches(request);
  2181. /* Not allowed to fail! */
  2182. WARN(ret, "*_ring_flush_all_caches failed: %d!\n", ret);
  2183. }
  2184. trace_i915_gem_request_add(request);
  2185. request->head = request_start;
  2186. /* Whilst this request exists, batch_obj will be on the
  2187. * active_list, and so will hold the active reference. Only when this
  2188. * request is retired will the the batch_obj be moved onto the
  2189. * inactive_list and lose its active reference. Hence we do not need
  2190. * to explicitly hold another reference here.
  2191. */
  2192. request->batch_obj = obj;
  2193. /* Seal the request and mark it as pending execution. Note that
  2194. * we may inspect this state, without holding any locks, during
  2195. * hangcheck. Hence we apply the barrier to ensure that we do not
  2196. * see a more recent value in the hws than we are tracking.
  2197. */
  2198. request->emitted_jiffies = jiffies;
  2199. request->previous_seqno = engine->last_submitted_seqno;
  2200. smp_store_mb(engine->last_submitted_seqno, request->seqno);
  2201. list_add_tail(&request->list, &engine->request_list);
  2202. /* Record the position of the start of the request so that
  2203. * should we detect the updated seqno part-way through the
  2204. * GPU processing the request, we never over-estimate the
  2205. * position of the head.
  2206. */
  2207. request->postfix = intel_ring_get_tail(ringbuf);
  2208. if (i915.enable_execlists)
  2209. ret = engine->emit_request(request);
  2210. else {
  2211. ret = engine->add_request(request);
  2212. request->tail = intel_ring_get_tail(ringbuf);
  2213. }
  2214. /* Not allowed to fail! */
  2215. WARN(ret, "emit|add_request failed: %d!\n", ret);
  2216. i915_queue_hangcheck(engine->i915);
  2217. queue_delayed_work(dev_priv->wq,
  2218. &dev_priv->mm.retire_work,
  2219. round_jiffies_up_relative(HZ));
  2220. intel_mark_busy(dev_priv);
  2221. /* Sanity check that the reserved size was large enough. */
  2222. ret = intel_ring_get_tail(ringbuf) - request_start;
  2223. if (ret < 0)
  2224. ret += ringbuf->size;
  2225. WARN_ONCE(ret > reserved_tail,
  2226. "Not enough space reserved (%d bytes) "
  2227. "for adding the request (%d bytes)\n",
  2228. reserved_tail, ret);
  2229. }
  2230. static bool i915_context_is_banned(struct drm_i915_private *dev_priv,
  2231. const struct i915_gem_context *ctx)
  2232. {
  2233. unsigned long elapsed;
  2234. elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
  2235. if (ctx->hang_stats.banned)
  2236. return true;
  2237. if (ctx->hang_stats.ban_period_seconds &&
  2238. elapsed <= ctx->hang_stats.ban_period_seconds) {
  2239. if (!i915_gem_context_is_default(ctx)) {
  2240. DRM_DEBUG("context hanging too fast, banning!\n");
  2241. return true;
  2242. } else if (i915_stop_ring_allow_ban(dev_priv)) {
  2243. if (i915_stop_ring_allow_warn(dev_priv))
  2244. DRM_ERROR("gpu hanging too fast, banning!\n");
  2245. return true;
  2246. }
  2247. }
  2248. return false;
  2249. }
  2250. static void i915_set_reset_status(struct drm_i915_private *dev_priv,
  2251. struct i915_gem_context *ctx,
  2252. const bool guilty)
  2253. {
  2254. struct i915_ctx_hang_stats *hs;
  2255. if (WARN_ON(!ctx))
  2256. return;
  2257. hs = &ctx->hang_stats;
  2258. if (guilty) {
  2259. hs->banned = i915_context_is_banned(dev_priv, ctx);
  2260. hs->batch_active++;
  2261. hs->guilty_ts = get_seconds();
  2262. } else {
  2263. hs->batch_pending++;
  2264. }
  2265. }
  2266. void i915_gem_request_free(struct kref *req_ref)
  2267. {
  2268. struct drm_i915_gem_request *req = container_of(req_ref,
  2269. typeof(*req), ref);
  2270. kmem_cache_free(req->i915->requests, req);
  2271. }
  2272. static inline int
  2273. __i915_gem_request_alloc(struct intel_engine_cs *engine,
  2274. struct i915_gem_context *ctx,
  2275. struct drm_i915_gem_request **req_out)
  2276. {
  2277. struct drm_i915_private *dev_priv = engine->i915;
  2278. unsigned reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  2279. struct drm_i915_gem_request *req;
  2280. int ret;
  2281. if (!req_out)
  2282. return -EINVAL;
  2283. *req_out = NULL;
  2284. /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
  2285. * EIO if the GPU is already wedged, or EAGAIN to drop the struct_mutex
  2286. * and restart.
  2287. */
  2288. ret = i915_gem_check_wedge(reset_counter, dev_priv->mm.interruptible);
  2289. if (ret)
  2290. return ret;
  2291. req = kmem_cache_zalloc(dev_priv->requests, GFP_KERNEL);
  2292. if (req == NULL)
  2293. return -ENOMEM;
  2294. ret = i915_gem_get_seqno(engine->i915, &req->seqno);
  2295. if (ret)
  2296. goto err;
  2297. kref_init(&req->ref);
  2298. req->i915 = dev_priv;
  2299. req->engine = engine;
  2300. req->reset_counter = reset_counter;
  2301. req->ctx = ctx;
  2302. i915_gem_context_reference(req->ctx);
  2303. /*
  2304. * Reserve space in the ring buffer for all the commands required to
  2305. * eventually emit this request. This is to guarantee that the
  2306. * i915_add_request() call can't fail. Note that the reserve may need
  2307. * to be redone if the request is not actually submitted straight
  2308. * away, e.g. because a GPU scheduler has deferred it.
  2309. */
  2310. req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
  2311. if (i915.enable_execlists)
  2312. ret = intel_logical_ring_alloc_request_extras(req);
  2313. else
  2314. ret = intel_ring_alloc_request_extras(req);
  2315. if (ret)
  2316. goto err_ctx;
  2317. *req_out = req;
  2318. return 0;
  2319. err_ctx:
  2320. i915_gem_context_unreference(ctx);
  2321. err:
  2322. kmem_cache_free(dev_priv->requests, req);
  2323. return ret;
  2324. }
  2325. /**
  2326. * i915_gem_request_alloc - allocate a request structure
  2327. *
  2328. * @engine: engine that we wish to issue the request on.
  2329. * @ctx: context that the request will be associated with.
  2330. * This can be NULL if the request is not directly related to
  2331. * any specific user context, in which case this function will
  2332. * choose an appropriate context to use.
  2333. *
  2334. * Returns a pointer to the allocated request if successful,
  2335. * or an error code if not.
  2336. */
  2337. struct drm_i915_gem_request *
  2338. i915_gem_request_alloc(struct intel_engine_cs *engine,
  2339. struct i915_gem_context *ctx)
  2340. {
  2341. struct drm_i915_gem_request *req;
  2342. int err;
  2343. if (ctx == NULL)
  2344. ctx = engine->i915->kernel_context;
  2345. err = __i915_gem_request_alloc(engine, ctx, &req);
  2346. return err ? ERR_PTR(err) : req;
  2347. }
  2348. struct drm_i915_gem_request *
  2349. i915_gem_find_active_request(struct intel_engine_cs *engine)
  2350. {
  2351. struct drm_i915_gem_request *request;
  2352. list_for_each_entry(request, &engine->request_list, list) {
  2353. if (i915_gem_request_completed(request, false))
  2354. continue;
  2355. return request;
  2356. }
  2357. return NULL;
  2358. }
  2359. static void i915_gem_reset_engine_status(struct drm_i915_private *dev_priv,
  2360. struct intel_engine_cs *engine)
  2361. {
  2362. struct drm_i915_gem_request *request;
  2363. bool ring_hung;
  2364. request = i915_gem_find_active_request(engine);
  2365. if (request == NULL)
  2366. return;
  2367. ring_hung = engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
  2368. i915_set_reset_status(dev_priv, request->ctx, ring_hung);
  2369. list_for_each_entry_continue(request, &engine->request_list, list)
  2370. i915_set_reset_status(dev_priv, request->ctx, false);
  2371. }
  2372. static void i915_gem_reset_engine_cleanup(struct drm_i915_private *dev_priv,
  2373. struct intel_engine_cs *engine)
  2374. {
  2375. struct intel_ringbuffer *buffer;
  2376. while (!list_empty(&engine->active_list)) {
  2377. struct drm_i915_gem_object *obj;
  2378. obj = list_first_entry(&engine->active_list,
  2379. struct drm_i915_gem_object,
  2380. engine_list[engine->id]);
  2381. i915_gem_object_retire__read(obj, engine->id);
  2382. }
  2383. /*
  2384. * Clear the execlists queue up before freeing the requests, as those
  2385. * are the ones that keep the context and ringbuffer backing objects
  2386. * pinned in place.
  2387. */
  2388. if (i915.enable_execlists) {
  2389. /* Ensure irq handler finishes or is cancelled. */
  2390. tasklet_kill(&engine->irq_tasklet);
  2391. intel_execlists_cancel_requests(engine);
  2392. }
  2393. /*
  2394. * We must free the requests after all the corresponding objects have
  2395. * been moved off active lists. Which is the same order as the normal
  2396. * retire_requests function does. This is important if object hold
  2397. * implicit references on things like e.g. ppgtt address spaces through
  2398. * the request.
  2399. */
  2400. while (!list_empty(&engine->request_list)) {
  2401. struct drm_i915_gem_request *request;
  2402. request = list_first_entry(&engine->request_list,
  2403. struct drm_i915_gem_request,
  2404. list);
  2405. i915_gem_request_retire(request);
  2406. }
  2407. /* Having flushed all requests from all queues, we know that all
  2408. * ringbuffers must now be empty. However, since we do not reclaim
  2409. * all space when retiring the request (to prevent HEADs colliding
  2410. * with rapid ringbuffer wraparound) the amount of available space
  2411. * upon reset is less than when we start. Do one more pass over
  2412. * all the ringbuffers to reset last_retired_head.
  2413. */
  2414. list_for_each_entry(buffer, &engine->buffers, link) {
  2415. buffer->last_retired_head = buffer->tail;
  2416. intel_ring_update_space(buffer);
  2417. }
  2418. intel_ring_init_seqno(engine, engine->last_submitted_seqno);
  2419. }
  2420. void i915_gem_reset(struct drm_device *dev)
  2421. {
  2422. struct drm_i915_private *dev_priv = dev->dev_private;
  2423. struct intel_engine_cs *engine;
  2424. /*
  2425. * Before we free the objects from the requests, we need to inspect
  2426. * them for finding the guilty party. As the requests only borrow
  2427. * their reference to the objects, the inspection must be done first.
  2428. */
  2429. for_each_engine(engine, dev_priv)
  2430. i915_gem_reset_engine_status(dev_priv, engine);
  2431. for_each_engine(engine, dev_priv)
  2432. i915_gem_reset_engine_cleanup(dev_priv, engine);
  2433. i915_gem_context_reset(dev);
  2434. i915_gem_restore_fences(dev);
  2435. WARN_ON(i915_verify_lists(dev));
  2436. }
  2437. /**
  2438. * This function clears the request list as sequence numbers are passed.
  2439. */
  2440. void
  2441. i915_gem_retire_requests_ring(struct intel_engine_cs *engine)
  2442. {
  2443. WARN_ON(i915_verify_lists(engine->dev));
  2444. /* Retire requests first as we use it above for the early return.
  2445. * If we retire requests last, we may use a later seqno and so clear
  2446. * the requests lists without clearing the active list, leading to
  2447. * confusion.
  2448. */
  2449. while (!list_empty(&engine->request_list)) {
  2450. struct drm_i915_gem_request *request;
  2451. request = list_first_entry(&engine->request_list,
  2452. struct drm_i915_gem_request,
  2453. list);
  2454. if (!i915_gem_request_completed(request, true))
  2455. break;
  2456. i915_gem_request_retire(request);
  2457. }
  2458. /* Move any buffers on the active list that are no longer referenced
  2459. * by the ringbuffer to the flushing/inactive lists as appropriate,
  2460. * before we free the context associated with the requests.
  2461. */
  2462. while (!list_empty(&engine->active_list)) {
  2463. struct drm_i915_gem_object *obj;
  2464. obj = list_first_entry(&engine->active_list,
  2465. struct drm_i915_gem_object,
  2466. engine_list[engine->id]);
  2467. if (!list_empty(&obj->last_read_req[engine->id]->list))
  2468. break;
  2469. i915_gem_object_retire__read(obj, engine->id);
  2470. }
  2471. if (unlikely(engine->trace_irq_req &&
  2472. i915_gem_request_completed(engine->trace_irq_req, true))) {
  2473. engine->irq_put(engine);
  2474. i915_gem_request_assign(&engine->trace_irq_req, NULL);
  2475. }
  2476. WARN_ON(i915_verify_lists(engine->dev));
  2477. }
  2478. bool
  2479. i915_gem_retire_requests(struct drm_i915_private *dev_priv)
  2480. {
  2481. struct intel_engine_cs *engine;
  2482. bool idle = true;
  2483. for_each_engine(engine, dev_priv) {
  2484. i915_gem_retire_requests_ring(engine);
  2485. idle &= list_empty(&engine->request_list);
  2486. if (i915.enable_execlists) {
  2487. spin_lock_bh(&engine->execlist_lock);
  2488. idle &= list_empty(&engine->execlist_queue);
  2489. spin_unlock_bh(&engine->execlist_lock);
  2490. }
  2491. }
  2492. if (idle)
  2493. mod_delayed_work(dev_priv->wq,
  2494. &dev_priv->mm.idle_work,
  2495. msecs_to_jiffies(100));
  2496. return idle;
  2497. }
  2498. static void
  2499. i915_gem_retire_work_handler(struct work_struct *work)
  2500. {
  2501. struct drm_i915_private *dev_priv =
  2502. container_of(work, typeof(*dev_priv), mm.retire_work.work);
  2503. struct drm_device *dev = dev_priv->dev;
  2504. bool idle;
  2505. /* Come back later if the device is busy... */
  2506. idle = false;
  2507. if (mutex_trylock(&dev->struct_mutex)) {
  2508. idle = i915_gem_retire_requests(dev_priv);
  2509. mutex_unlock(&dev->struct_mutex);
  2510. }
  2511. if (!idle)
  2512. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  2513. round_jiffies_up_relative(HZ));
  2514. }
  2515. static void
  2516. i915_gem_idle_work_handler(struct work_struct *work)
  2517. {
  2518. struct drm_i915_private *dev_priv =
  2519. container_of(work, typeof(*dev_priv), mm.idle_work.work);
  2520. struct drm_device *dev = dev_priv->dev;
  2521. struct intel_engine_cs *engine;
  2522. for_each_engine(engine, dev_priv)
  2523. if (!list_empty(&engine->request_list))
  2524. return;
  2525. /* we probably should sync with hangcheck here, using cancel_work_sync.
  2526. * Also locking seems to be fubar here, engine->request_list is protected
  2527. * by dev->struct_mutex. */
  2528. intel_mark_idle(dev_priv);
  2529. if (mutex_trylock(&dev->struct_mutex)) {
  2530. for_each_engine(engine, dev_priv)
  2531. i915_gem_batch_pool_fini(&engine->batch_pool);
  2532. mutex_unlock(&dev->struct_mutex);
  2533. }
  2534. }
  2535. /**
  2536. * Ensures that an object will eventually get non-busy by flushing any required
  2537. * write domains, emitting any outstanding lazy request and retiring and
  2538. * completed requests.
  2539. */
  2540. static int
  2541. i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
  2542. {
  2543. int i;
  2544. if (!obj->active)
  2545. return 0;
  2546. for (i = 0; i < I915_NUM_ENGINES; i++) {
  2547. struct drm_i915_gem_request *req;
  2548. req = obj->last_read_req[i];
  2549. if (req == NULL)
  2550. continue;
  2551. if (i915_gem_request_completed(req, true))
  2552. i915_gem_object_retire__read(obj, i);
  2553. }
  2554. return 0;
  2555. }
  2556. /**
  2557. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2558. * @DRM_IOCTL_ARGS: standard ioctl arguments
  2559. *
  2560. * Returns 0 if successful, else an error is returned with the remaining time in
  2561. * the timeout parameter.
  2562. * -ETIME: object is still busy after timeout
  2563. * -ERESTARTSYS: signal interrupted the wait
  2564. * -ENONENT: object doesn't exist
  2565. * Also possible, but rare:
  2566. * -EAGAIN: GPU wedged
  2567. * -ENOMEM: damn
  2568. * -ENODEV: Internal IRQ fail
  2569. * -E?: The add request failed
  2570. *
  2571. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2572. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2573. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2574. * without holding struct_mutex the object may become re-busied before this
  2575. * function completes. A similar but shorter * race condition exists in the busy
  2576. * ioctl
  2577. */
  2578. int
  2579. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2580. {
  2581. struct drm_i915_gem_wait *args = data;
  2582. struct drm_i915_gem_object *obj;
  2583. struct drm_i915_gem_request *req[I915_NUM_ENGINES];
  2584. int i, n = 0;
  2585. int ret;
  2586. if (args->flags != 0)
  2587. return -EINVAL;
  2588. ret = i915_mutex_lock_interruptible(dev);
  2589. if (ret)
  2590. return ret;
  2591. obj = to_intel_bo(drm_gem_object_lookup(file, args->bo_handle));
  2592. if (&obj->base == NULL) {
  2593. mutex_unlock(&dev->struct_mutex);
  2594. return -ENOENT;
  2595. }
  2596. /* Need to make sure the object gets inactive eventually. */
  2597. ret = i915_gem_object_flush_active(obj);
  2598. if (ret)
  2599. goto out;
  2600. if (!obj->active)
  2601. goto out;
  2602. /* Do this after OLR check to make sure we make forward progress polling
  2603. * on this IOCTL with a timeout == 0 (like busy ioctl)
  2604. */
  2605. if (args->timeout_ns == 0) {
  2606. ret = -ETIME;
  2607. goto out;
  2608. }
  2609. drm_gem_object_unreference(&obj->base);
  2610. for (i = 0; i < I915_NUM_ENGINES; i++) {
  2611. if (obj->last_read_req[i] == NULL)
  2612. continue;
  2613. req[n++] = i915_gem_request_reference(obj->last_read_req[i]);
  2614. }
  2615. mutex_unlock(&dev->struct_mutex);
  2616. for (i = 0; i < n; i++) {
  2617. if (ret == 0)
  2618. ret = __i915_wait_request(req[i], true,
  2619. args->timeout_ns > 0 ? &args->timeout_ns : NULL,
  2620. to_rps_client(file));
  2621. i915_gem_request_unreference(req[i]);
  2622. }
  2623. return ret;
  2624. out:
  2625. drm_gem_object_unreference(&obj->base);
  2626. mutex_unlock(&dev->struct_mutex);
  2627. return ret;
  2628. }
  2629. static int
  2630. __i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2631. struct intel_engine_cs *to,
  2632. struct drm_i915_gem_request *from_req,
  2633. struct drm_i915_gem_request **to_req)
  2634. {
  2635. struct intel_engine_cs *from;
  2636. int ret;
  2637. from = i915_gem_request_get_engine(from_req);
  2638. if (to == from)
  2639. return 0;
  2640. if (i915_gem_request_completed(from_req, true))
  2641. return 0;
  2642. if (!i915_semaphore_is_enabled(to_i915(obj->base.dev))) {
  2643. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  2644. ret = __i915_wait_request(from_req,
  2645. i915->mm.interruptible,
  2646. NULL,
  2647. &i915->rps.semaphores);
  2648. if (ret)
  2649. return ret;
  2650. i915_gem_object_retire_request(obj, from_req);
  2651. } else {
  2652. int idx = intel_ring_sync_index(from, to);
  2653. u32 seqno = i915_gem_request_get_seqno(from_req);
  2654. WARN_ON(!to_req);
  2655. if (seqno <= from->semaphore.sync_seqno[idx])
  2656. return 0;
  2657. if (*to_req == NULL) {
  2658. struct drm_i915_gem_request *req;
  2659. req = i915_gem_request_alloc(to, NULL);
  2660. if (IS_ERR(req))
  2661. return PTR_ERR(req);
  2662. *to_req = req;
  2663. }
  2664. trace_i915_gem_ring_sync_to(*to_req, from, from_req);
  2665. ret = to->semaphore.sync_to(*to_req, from, seqno);
  2666. if (ret)
  2667. return ret;
  2668. /* We use last_read_req because sync_to()
  2669. * might have just caused seqno wrap under
  2670. * the radar.
  2671. */
  2672. from->semaphore.sync_seqno[idx] =
  2673. i915_gem_request_get_seqno(obj->last_read_req[from->id]);
  2674. }
  2675. return 0;
  2676. }
  2677. /**
  2678. * i915_gem_object_sync - sync an object to a ring.
  2679. *
  2680. * @obj: object which may be in use on another ring.
  2681. * @to: ring we wish to use the object on. May be NULL.
  2682. * @to_req: request we wish to use the object for. See below.
  2683. * This will be allocated and returned if a request is
  2684. * required but not passed in.
  2685. *
  2686. * This code is meant to abstract object synchronization with the GPU.
  2687. * Calling with NULL implies synchronizing the object with the CPU
  2688. * rather than a particular GPU ring. Conceptually we serialise writes
  2689. * between engines inside the GPU. We only allow one engine to write
  2690. * into a buffer at any time, but multiple readers. To ensure each has
  2691. * a coherent view of memory, we must:
  2692. *
  2693. * - If there is an outstanding write request to the object, the new
  2694. * request must wait for it to complete (either CPU or in hw, requests
  2695. * on the same ring will be naturally ordered).
  2696. *
  2697. * - If we are a write request (pending_write_domain is set), the new
  2698. * request must wait for outstanding read requests to complete.
  2699. *
  2700. * For CPU synchronisation (NULL to) no request is required. For syncing with
  2701. * rings to_req must be non-NULL. However, a request does not have to be
  2702. * pre-allocated. If *to_req is NULL and sync commands will be emitted then a
  2703. * request will be allocated automatically and returned through *to_req. Note
  2704. * that it is not guaranteed that commands will be emitted (because the system
  2705. * might already be idle). Hence there is no need to create a request that
  2706. * might never have any work submitted. Note further that if a request is
  2707. * returned in *to_req, it is the responsibility of the caller to submit
  2708. * that request (after potentially adding more work to it).
  2709. *
  2710. * Returns 0 if successful, else propagates up the lower layer error.
  2711. */
  2712. int
  2713. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2714. struct intel_engine_cs *to,
  2715. struct drm_i915_gem_request **to_req)
  2716. {
  2717. const bool readonly = obj->base.pending_write_domain == 0;
  2718. struct drm_i915_gem_request *req[I915_NUM_ENGINES];
  2719. int ret, i, n;
  2720. if (!obj->active)
  2721. return 0;
  2722. if (to == NULL)
  2723. return i915_gem_object_wait_rendering(obj, readonly);
  2724. n = 0;
  2725. if (readonly) {
  2726. if (obj->last_write_req)
  2727. req[n++] = obj->last_write_req;
  2728. } else {
  2729. for (i = 0; i < I915_NUM_ENGINES; i++)
  2730. if (obj->last_read_req[i])
  2731. req[n++] = obj->last_read_req[i];
  2732. }
  2733. for (i = 0; i < n; i++) {
  2734. ret = __i915_gem_object_sync(obj, to, req[i], to_req);
  2735. if (ret)
  2736. return ret;
  2737. }
  2738. return 0;
  2739. }
  2740. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  2741. {
  2742. u32 old_write_domain, old_read_domains;
  2743. /* Force a pagefault for domain tracking on next user access */
  2744. i915_gem_release_mmap(obj);
  2745. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2746. return;
  2747. old_read_domains = obj->base.read_domains;
  2748. old_write_domain = obj->base.write_domain;
  2749. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  2750. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  2751. trace_i915_gem_object_change_domain(obj,
  2752. old_read_domains,
  2753. old_write_domain);
  2754. }
  2755. static void __i915_vma_iounmap(struct i915_vma *vma)
  2756. {
  2757. GEM_BUG_ON(vma->pin_count);
  2758. if (vma->iomap == NULL)
  2759. return;
  2760. io_mapping_unmap(vma->iomap);
  2761. vma->iomap = NULL;
  2762. }
  2763. static int __i915_vma_unbind(struct i915_vma *vma, bool wait)
  2764. {
  2765. struct drm_i915_gem_object *obj = vma->obj;
  2766. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2767. int ret;
  2768. if (list_empty(&vma->obj_link))
  2769. return 0;
  2770. if (!drm_mm_node_allocated(&vma->node)) {
  2771. i915_gem_vma_destroy(vma);
  2772. return 0;
  2773. }
  2774. if (vma->pin_count)
  2775. return -EBUSY;
  2776. BUG_ON(obj->pages == NULL);
  2777. if (wait) {
  2778. ret = i915_gem_object_wait_rendering(obj, false);
  2779. if (ret)
  2780. return ret;
  2781. }
  2782. if (vma->is_ggtt && vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
  2783. i915_gem_object_finish_gtt(obj);
  2784. /* release the fence reg _after_ flushing */
  2785. ret = i915_gem_object_put_fence(obj);
  2786. if (ret)
  2787. return ret;
  2788. __i915_vma_iounmap(vma);
  2789. }
  2790. trace_i915_vma_unbind(vma);
  2791. vma->vm->unbind_vma(vma);
  2792. vma->bound = 0;
  2793. list_del_init(&vma->vm_link);
  2794. if (vma->is_ggtt) {
  2795. if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
  2796. obj->map_and_fenceable = false;
  2797. } else if (vma->ggtt_view.pages) {
  2798. sg_free_table(vma->ggtt_view.pages);
  2799. kfree(vma->ggtt_view.pages);
  2800. }
  2801. vma->ggtt_view.pages = NULL;
  2802. }
  2803. drm_mm_remove_node(&vma->node);
  2804. i915_gem_vma_destroy(vma);
  2805. /* Since the unbound list is global, only move to that list if
  2806. * no more VMAs exist. */
  2807. if (list_empty(&obj->vma_list))
  2808. list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  2809. /* And finally now the object is completely decoupled from this vma,
  2810. * we can drop its hold on the backing storage and allow it to be
  2811. * reaped by the shrinker.
  2812. */
  2813. i915_gem_object_unpin_pages(obj);
  2814. return 0;
  2815. }
  2816. int i915_vma_unbind(struct i915_vma *vma)
  2817. {
  2818. return __i915_vma_unbind(vma, true);
  2819. }
  2820. int __i915_vma_unbind_no_wait(struct i915_vma *vma)
  2821. {
  2822. return __i915_vma_unbind(vma, false);
  2823. }
  2824. int i915_gpu_idle(struct drm_device *dev)
  2825. {
  2826. struct drm_i915_private *dev_priv = dev->dev_private;
  2827. struct intel_engine_cs *engine;
  2828. int ret;
  2829. /* Flush everything onto the inactive list. */
  2830. for_each_engine(engine, dev_priv) {
  2831. if (!i915.enable_execlists) {
  2832. struct drm_i915_gem_request *req;
  2833. req = i915_gem_request_alloc(engine, NULL);
  2834. if (IS_ERR(req))
  2835. return PTR_ERR(req);
  2836. ret = i915_switch_context(req);
  2837. i915_add_request_no_flush(req);
  2838. if (ret)
  2839. return ret;
  2840. }
  2841. ret = intel_engine_idle(engine);
  2842. if (ret)
  2843. return ret;
  2844. }
  2845. WARN_ON(i915_verify_lists(dev));
  2846. return 0;
  2847. }
  2848. static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
  2849. unsigned long cache_level)
  2850. {
  2851. struct drm_mm_node *gtt_space = &vma->node;
  2852. struct drm_mm_node *other;
  2853. /*
  2854. * On some machines we have to be careful when putting differing types
  2855. * of snoopable memory together to avoid the prefetcher crossing memory
  2856. * domains and dying. During vm initialisation, we decide whether or not
  2857. * these constraints apply and set the drm_mm.color_adjust
  2858. * appropriately.
  2859. */
  2860. if (vma->vm->mm.color_adjust == NULL)
  2861. return true;
  2862. if (!drm_mm_node_allocated(gtt_space))
  2863. return true;
  2864. if (list_empty(&gtt_space->node_list))
  2865. return true;
  2866. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  2867. if (other->allocated && !other->hole_follows && other->color != cache_level)
  2868. return false;
  2869. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  2870. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  2871. return false;
  2872. return true;
  2873. }
  2874. /**
  2875. * Finds free space in the GTT aperture and binds the object or a view of it
  2876. * there.
  2877. */
  2878. static struct i915_vma *
  2879. i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
  2880. struct i915_address_space *vm,
  2881. const struct i915_ggtt_view *ggtt_view,
  2882. unsigned alignment,
  2883. uint64_t flags)
  2884. {
  2885. struct drm_device *dev = obj->base.dev;
  2886. struct drm_i915_private *dev_priv = to_i915(dev);
  2887. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2888. u32 fence_alignment, unfenced_alignment;
  2889. u32 search_flag, alloc_flag;
  2890. u64 start, end;
  2891. u64 size, fence_size;
  2892. struct i915_vma *vma;
  2893. int ret;
  2894. if (i915_is_ggtt(vm)) {
  2895. u32 view_size;
  2896. if (WARN_ON(!ggtt_view))
  2897. return ERR_PTR(-EINVAL);
  2898. view_size = i915_ggtt_view_size(obj, ggtt_view);
  2899. fence_size = i915_gem_get_gtt_size(dev,
  2900. view_size,
  2901. obj->tiling_mode);
  2902. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2903. view_size,
  2904. obj->tiling_mode,
  2905. true);
  2906. unfenced_alignment = i915_gem_get_gtt_alignment(dev,
  2907. view_size,
  2908. obj->tiling_mode,
  2909. false);
  2910. size = flags & PIN_MAPPABLE ? fence_size : view_size;
  2911. } else {
  2912. fence_size = i915_gem_get_gtt_size(dev,
  2913. obj->base.size,
  2914. obj->tiling_mode);
  2915. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2916. obj->base.size,
  2917. obj->tiling_mode,
  2918. true);
  2919. unfenced_alignment =
  2920. i915_gem_get_gtt_alignment(dev,
  2921. obj->base.size,
  2922. obj->tiling_mode,
  2923. false);
  2924. size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
  2925. }
  2926. start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
  2927. end = vm->total;
  2928. if (flags & PIN_MAPPABLE)
  2929. end = min_t(u64, end, ggtt->mappable_end);
  2930. if (flags & PIN_ZONE_4G)
  2931. end = min_t(u64, end, (1ULL << 32) - PAGE_SIZE);
  2932. if (alignment == 0)
  2933. alignment = flags & PIN_MAPPABLE ? fence_alignment :
  2934. unfenced_alignment;
  2935. if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
  2936. DRM_DEBUG("Invalid object (view type=%u) alignment requested %u\n",
  2937. ggtt_view ? ggtt_view->type : 0,
  2938. alignment);
  2939. return ERR_PTR(-EINVAL);
  2940. }
  2941. /* If binding the object/GGTT view requires more space than the entire
  2942. * aperture has, reject it early before evicting everything in a vain
  2943. * attempt to find space.
  2944. */
  2945. if (size > end) {
  2946. DRM_DEBUG("Attempting to bind an object (view type=%u) larger than the aperture: size=%llu > %s aperture=%llu\n",
  2947. ggtt_view ? ggtt_view->type : 0,
  2948. size,
  2949. flags & PIN_MAPPABLE ? "mappable" : "total",
  2950. end);
  2951. return ERR_PTR(-E2BIG);
  2952. }
  2953. ret = i915_gem_object_get_pages(obj);
  2954. if (ret)
  2955. return ERR_PTR(ret);
  2956. i915_gem_object_pin_pages(obj);
  2957. vma = ggtt_view ? i915_gem_obj_lookup_or_create_ggtt_vma(obj, ggtt_view) :
  2958. i915_gem_obj_lookup_or_create_vma(obj, vm);
  2959. if (IS_ERR(vma))
  2960. goto err_unpin;
  2961. if (flags & PIN_OFFSET_FIXED) {
  2962. uint64_t offset = flags & PIN_OFFSET_MASK;
  2963. if (offset & (alignment - 1) || offset + size > end) {
  2964. ret = -EINVAL;
  2965. goto err_free_vma;
  2966. }
  2967. vma->node.start = offset;
  2968. vma->node.size = size;
  2969. vma->node.color = obj->cache_level;
  2970. ret = drm_mm_reserve_node(&vm->mm, &vma->node);
  2971. if (ret) {
  2972. ret = i915_gem_evict_for_vma(vma);
  2973. if (ret == 0)
  2974. ret = drm_mm_reserve_node(&vm->mm, &vma->node);
  2975. }
  2976. if (ret)
  2977. goto err_free_vma;
  2978. } else {
  2979. if (flags & PIN_HIGH) {
  2980. search_flag = DRM_MM_SEARCH_BELOW;
  2981. alloc_flag = DRM_MM_CREATE_TOP;
  2982. } else {
  2983. search_flag = DRM_MM_SEARCH_DEFAULT;
  2984. alloc_flag = DRM_MM_CREATE_DEFAULT;
  2985. }
  2986. search_free:
  2987. ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
  2988. size, alignment,
  2989. obj->cache_level,
  2990. start, end,
  2991. search_flag,
  2992. alloc_flag);
  2993. if (ret) {
  2994. ret = i915_gem_evict_something(dev, vm, size, alignment,
  2995. obj->cache_level,
  2996. start, end,
  2997. flags);
  2998. if (ret == 0)
  2999. goto search_free;
  3000. goto err_free_vma;
  3001. }
  3002. }
  3003. if (WARN_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level))) {
  3004. ret = -EINVAL;
  3005. goto err_remove_node;
  3006. }
  3007. trace_i915_vma_bind(vma, flags);
  3008. ret = i915_vma_bind(vma, obj->cache_level, flags);
  3009. if (ret)
  3010. goto err_remove_node;
  3011. list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
  3012. list_add_tail(&vma->vm_link, &vm->inactive_list);
  3013. return vma;
  3014. err_remove_node:
  3015. drm_mm_remove_node(&vma->node);
  3016. err_free_vma:
  3017. i915_gem_vma_destroy(vma);
  3018. vma = ERR_PTR(ret);
  3019. err_unpin:
  3020. i915_gem_object_unpin_pages(obj);
  3021. return vma;
  3022. }
  3023. bool
  3024. i915_gem_clflush_object(struct drm_i915_gem_object *obj,
  3025. bool force)
  3026. {
  3027. /* If we don't have a page list set up, then we're not pinned
  3028. * to GPU, and we can ignore the cache flush because it'll happen
  3029. * again at bind time.
  3030. */
  3031. if (obj->pages == NULL)
  3032. return false;
  3033. /*
  3034. * Stolen memory is always coherent with the GPU as it is explicitly
  3035. * marked as wc by the system, or the system is cache-coherent.
  3036. */
  3037. if (obj->stolen || obj->phys_handle)
  3038. return false;
  3039. /* If the GPU is snooping the contents of the CPU cache,
  3040. * we do not need to manually clear the CPU cache lines. However,
  3041. * the caches are only snooped when the render cache is
  3042. * flushed/invalidated. As we always have to emit invalidations
  3043. * and flushes when moving into and out of the RENDER domain, correct
  3044. * snooping behaviour occurs naturally as the result of our domain
  3045. * tracking.
  3046. */
  3047. if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
  3048. obj->cache_dirty = true;
  3049. return false;
  3050. }
  3051. trace_i915_gem_object_clflush(obj);
  3052. drm_clflush_sg(obj->pages);
  3053. obj->cache_dirty = false;
  3054. return true;
  3055. }
  3056. /** Flushes the GTT write domain for the object if it's dirty. */
  3057. static void
  3058. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  3059. {
  3060. uint32_t old_write_domain;
  3061. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  3062. return;
  3063. /* No actual flushing is required for the GTT write domain. Writes
  3064. * to it immediately go to main memory as far as we know, so there's
  3065. * no chipset flush. It also doesn't land in render cache.
  3066. *
  3067. * However, we do have to enforce the order so that all writes through
  3068. * the GTT land before any writes to the device, such as updates to
  3069. * the GATT itself.
  3070. */
  3071. wmb();
  3072. old_write_domain = obj->base.write_domain;
  3073. obj->base.write_domain = 0;
  3074. intel_fb_obj_flush(obj, false, ORIGIN_GTT);
  3075. trace_i915_gem_object_change_domain(obj,
  3076. obj->base.read_domains,
  3077. old_write_domain);
  3078. }
  3079. /** Flushes the CPU write domain for the object if it's dirty. */
  3080. static void
  3081. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  3082. {
  3083. uint32_t old_write_domain;
  3084. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  3085. return;
  3086. if (i915_gem_clflush_object(obj, obj->pin_display))
  3087. i915_gem_chipset_flush(to_i915(obj->base.dev));
  3088. old_write_domain = obj->base.write_domain;
  3089. obj->base.write_domain = 0;
  3090. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  3091. trace_i915_gem_object_change_domain(obj,
  3092. obj->base.read_domains,
  3093. old_write_domain);
  3094. }
  3095. /**
  3096. * Moves a single object to the GTT read, and possibly write domain.
  3097. *
  3098. * This function returns when the move is complete, including waiting on
  3099. * flushes to occur.
  3100. */
  3101. int
  3102. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  3103. {
  3104. struct drm_device *dev = obj->base.dev;
  3105. struct drm_i915_private *dev_priv = to_i915(dev);
  3106. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  3107. uint32_t old_write_domain, old_read_domains;
  3108. struct i915_vma *vma;
  3109. int ret;
  3110. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  3111. return 0;
  3112. ret = i915_gem_object_wait_rendering(obj, !write);
  3113. if (ret)
  3114. return ret;
  3115. /* Flush and acquire obj->pages so that we are coherent through
  3116. * direct access in memory with previous cached writes through
  3117. * shmemfs and that our cache domain tracking remains valid.
  3118. * For example, if the obj->filp was moved to swap without us
  3119. * being notified and releasing the pages, we would mistakenly
  3120. * continue to assume that the obj remained out of the CPU cached
  3121. * domain.
  3122. */
  3123. ret = i915_gem_object_get_pages(obj);
  3124. if (ret)
  3125. return ret;
  3126. i915_gem_object_flush_cpu_write_domain(obj);
  3127. /* Serialise direct access to this object with the barriers for
  3128. * coherent writes from the GPU, by effectively invalidating the
  3129. * GTT domain upon first access.
  3130. */
  3131. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  3132. mb();
  3133. old_write_domain = obj->base.write_domain;
  3134. old_read_domains = obj->base.read_domains;
  3135. /* It should now be out of any other write domains, and we can update
  3136. * the domain values for our changes.
  3137. */
  3138. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  3139. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3140. if (write) {
  3141. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  3142. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  3143. obj->dirty = 1;
  3144. }
  3145. trace_i915_gem_object_change_domain(obj,
  3146. old_read_domains,
  3147. old_write_domain);
  3148. /* And bump the LRU for this access */
  3149. vma = i915_gem_obj_to_ggtt(obj);
  3150. if (vma && drm_mm_node_allocated(&vma->node) && !obj->active)
  3151. list_move_tail(&vma->vm_link,
  3152. &ggtt->base.inactive_list);
  3153. return 0;
  3154. }
  3155. /**
  3156. * Changes the cache-level of an object across all VMA.
  3157. *
  3158. * After this function returns, the object will be in the new cache-level
  3159. * across all GTT and the contents of the backing storage will be coherent,
  3160. * with respect to the new cache-level. In order to keep the backing storage
  3161. * coherent for all users, we only allow a single cache level to be set
  3162. * globally on the object and prevent it from being changed whilst the
  3163. * hardware is reading from the object. That is if the object is currently
  3164. * on the scanout it will be set to uncached (or equivalent display
  3165. * cache coherency) and all non-MOCS GPU access will also be uncached so
  3166. * that all direct access to the scanout remains coherent.
  3167. */
  3168. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  3169. enum i915_cache_level cache_level)
  3170. {
  3171. struct drm_device *dev = obj->base.dev;
  3172. struct i915_vma *vma, *next;
  3173. bool bound = false;
  3174. int ret = 0;
  3175. if (obj->cache_level == cache_level)
  3176. goto out;
  3177. /* Inspect the list of currently bound VMA and unbind any that would
  3178. * be invalid given the new cache-level. This is principally to
  3179. * catch the issue of the CS prefetch crossing page boundaries and
  3180. * reading an invalid PTE on older architectures.
  3181. */
  3182. list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
  3183. if (!drm_mm_node_allocated(&vma->node))
  3184. continue;
  3185. if (vma->pin_count) {
  3186. DRM_DEBUG("can not change the cache level of pinned objects\n");
  3187. return -EBUSY;
  3188. }
  3189. if (!i915_gem_valid_gtt_space(vma, cache_level)) {
  3190. ret = i915_vma_unbind(vma);
  3191. if (ret)
  3192. return ret;
  3193. } else
  3194. bound = true;
  3195. }
  3196. /* We can reuse the existing drm_mm nodes but need to change the
  3197. * cache-level on the PTE. We could simply unbind them all and
  3198. * rebind with the correct cache-level on next use. However since
  3199. * we already have a valid slot, dma mapping, pages etc, we may as
  3200. * rewrite the PTE in the belief that doing so tramples upon less
  3201. * state and so involves less work.
  3202. */
  3203. if (bound) {
  3204. /* Before we change the PTE, the GPU must not be accessing it.
  3205. * If we wait upon the object, we know that all the bound
  3206. * VMA are no longer active.
  3207. */
  3208. ret = i915_gem_object_wait_rendering(obj, false);
  3209. if (ret)
  3210. return ret;
  3211. if (!HAS_LLC(dev) && cache_level != I915_CACHE_NONE) {
  3212. /* Access to snoopable pages through the GTT is
  3213. * incoherent and on some machines causes a hard
  3214. * lockup. Relinquish the CPU mmaping to force
  3215. * userspace to refault in the pages and we can
  3216. * then double check if the GTT mapping is still
  3217. * valid for that pointer access.
  3218. */
  3219. i915_gem_release_mmap(obj);
  3220. /* As we no longer need a fence for GTT access,
  3221. * we can relinquish it now (and so prevent having
  3222. * to steal a fence from someone else on the next
  3223. * fence request). Note GPU activity would have
  3224. * dropped the fence as all snoopable access is
  3225. * supposed to be linear.
  3226. */
  3227. ret = i915_gem_object_put_fence(obj);
  3228. if (ret)
  3229. return ret;
  3230. } else {
  3231. /* We either have incoherent backing store and
  3232. * so no GTT access or the architecture is fully
  3233. * coherent. In such cases, existing GTT mmaps
  3234. * ignore the cache bit in the PTE and we can
  3235. * rewrite it without confusing the GPU or having
  3236. * to force userspace to fault back in its mmaps.
  3237. */
  3238. }
  3239. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  3240. if (!drm_mm_node_allocated(&vma->node))
  3241. continue;
  3242. ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
  3243. if (ret)
  3244. return ret;
  3245. }
  3246. }
  3247. list_for_each_entry(vma, &obj->vma_list, obj_link)
  3248. vma->node.color = cache_level;
  3249. obj->cache_level = cache_level;
  3250. out:
  3251. /* Flush the dirty CPU caches to the backing storage so that the
  3252. * object is now coherent at its new cache level (with respect
  3253. * to the access domain).
  3254. */
  3255. if (obj->cache_dirty &&
  3256. obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
  3257. cpu_write_needs_clflush(obj)) {
  3258. if (i915_gem_clflush_object(obj, true))
  3259. i915_gem_chipset_flush(to_i915(obj->base.dev));
  3260. }
  3261. return 0;
  3262. }
  3263. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  3264. struct drm_file *file)
  3265. {
  3266. struct drm_i915_gem_caching *args = data;
  3267. struct drm_i915_gem_object *obj;
  3268. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  3269. if (&obj->base == NULL)
  3270. return -ENOENT;
  3271. switch (obj->cache_level) {
  3272. case I915_CACHE_LLC:
  3273. case I915_CACHE_L3_LLC:
  3274. args->caching = I915_CACHING_CACHED;
  3275. break;
  3276. case I915_CACHE_WT:
  3277. args->caching = I915_CACHING_DISPLAY;
  3278. break;
  3279. default:
  3280. args->caching = I915_CACHING_NONE;
  3281. break;
  3282. }
  3283. drm_gem_object_unreference_unlocked(&obj->base);
  3284. return 0;
  3285. }
  3286. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  3287. struct drm_file *file)
  3288. {
  3289. struct drm_i915_private *dev_priv = dev->dev_private;
  3290. struct drm_i915_gem_caching *args = data;
  3291. struct drm_i915_gem_object *obj;
  3292. enum i915_cache_level level;
  3293. int ret;
  3294. switch (args->caching) {
  3295. case I915_CACHING_NONE:
  3296. level = I915_CACHE_NONE;
  3297. break;
  3298. case I915_CACHING_CACHED:
  3299. /*
  3300. * Due to a HW issue on BXT A stepping, GPU stores via a
  3301. * snooped mapping may leave stale data in a corresponding CPU
  3302. * cacheline, whereas normally such cachelines would get
  3303. * invalidated.
  3304. */
  3305. if (!HAS_LLC(dev) && !HAS_SNOOP(dev))
  3306. return -ENODEV;
  3307. level = I915_CACHE_LLC;
  3308. break;
  3309. case I915_CACHING_DISPLAY:
  3310. level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
  3311. break;
  3312. default:
  3313. return -EINVAL;
  3314. }
  3315. intel_runtime_pm_get(dev_priv);
  3316. ret = i915_mutex_lock_interruptible(dev);
  3317. if (ret)
  3318. goto rpm_put;
  3319. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  3320. if (&obj->base == NULL) {
  3321. ret = -ENOENT;
  3322. goto unlock;
  3323. }
  3324. ret = i915_gem_object_set_cache_level(obj, level);
  3325. drm_gem_object_unreference(&obj->base);
  3326. unlock:
  3327. mutex_unlock(&dev->struct_mutex);
  3328. rpm_put:
  3329. intel_runtime_pm_put(dev_priv);
  3330. return ret;
  3331. }
  3332. /*
  3333. * Prepare buffer for display plane (scanout, cursors, etc).
  3334. * Can be called from an uninterruptible phase (modesetting) and allows
  3335. * any flushes to be pipelined (for pageflips).
  3336. */
  3337. int
  3338. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  3339. u32 alignment,
  3340. const struct i915_ggtt_view *view)
  3341. {
  3342. u32 old_read_domains, old_write_domain;
  3343. int ret;
  3344. /* Mark the pin_display early so that we account for the
  3345. * display coherency whilst setting up the cache domains.
  3346. */
  3347. obj->pin_display++;
  3348. /* The display engine is not coherent with the LLC cache on gen6. As
  3349. * a result, we make sure that the pinning that is about to occur is
  3350. * done with uncached PTEs. This is lowest common denominator for all
  3351. * chipsets.
  3352. *
  3353. * However for gen6+, we could do better by using the GFDT bit instead
  3354. * of uncaching, which would allow us to flush all the LLC-cached data
  3355. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  3356. */
  3357. ret = i915_gem_object_set_cache_level(obj,
  3358. HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
  3359. if (ret)
  3360. goto err_unpin_display;
  3361. /* As the user may map the buffer once pinned in the display plane
  3362. * (e.g. libkms for the bootup splash), we have to ensure that we
  3363. * always use map_and_fenceable for all scanout buffers.
  3364. */
  3365. ret = i915_gem_object_ggtt_pin(obj, view, alignment,
  3366. view->type == I915_GGTT_VIEW_NORMAL ?
  3367. PIN_MAPPABLE : 0);
  3368. if (ret)
  3369. goto err_unpin_display;
  3370. i915_gem_object_flush_cpu_write_domain(obj);
  3371. old_write_domain = obj->base.write_domain;
  3372. old_read_domains = obj->base.read_domains;
  3373. /* It should now be out of any other write domains, and we can update
  3374. * the domain values for our changes.
  3375. */
  3376. obj->base.write_domain = 0;
  3377. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3378. trace_i915_gem_object_change_domain(obj,
  3379. old_read_domains,
  3380. old_write_domain);
  3381. return 0;
  3382. err_unpin_display:
  3383. obj->pin_display--;
  3384. return ret;
  3385. }
  3386. void
  3387. i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj,
  3388. const struct i915_ggtt_view *view)
  3389. {
  3390. if (WARN_ON(obj->pin_display == 0))
  3391. return;
  3392. i915_gem_object_ggtt_unpin_view(obj, view);
  3393. obj->pin_display--;
  3394. }
  3395. /**
  3396. * Moves a single object to the CPU read, and possibly write domain.
  3397. *
  3398. * This function returns when the move is complete, including waiting on
  3399. * flushes to occur.
  3400. */
  3401. int
  3402. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3403. {
  3404. uint32_t old_write_domain, old_read_domains;
  3405. int ret;
  3406. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  3407. return 0;
  3408. ret = i915_gem_object_wait_rendering(obj, !write);
  3409. if (ret)
  3410. return ret;
  3411. i915_gem_object_flush_gtt_write_domain(obj);
  3412. old_write_domain = obj->base.write_domain;
  3413. old_read_domains = obj->base.read_domains;
  3414. /* Flush the CPU cache if it's still invalid. */
  3415. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3416. i915_gem_clflush_object(obj, false);
  3417. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3418. }
  3419. /* It should now be out of any other write domains, and we can update
  3420. * the domain values for our changes.
  3421. */
  3422. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  3423. /* If we're writing through the CPU, then the GPU read domains will
  3424. * need to be invalidated at next use.
  3425. */
  3426. if (write) {
  3427. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3428. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3429. }
  3430. trace_i915_gem_object_change_domain(obj,
  3431. old_read_domains,
  3432. old_write_domain);
  3433. return 0;
  3434. }
  3435. /* Throttle our rendering by waiting until the ring has completed our requests
  3436. * emitted over 20 msec ago.
  3437. *
  3438. * Note that if we were to use the current jiffies each time around the loop,
  3439. * we wouldn't escape the function with any frames outstanding if the time to
  3440. * render a frame was over 20ms.
  3441. *
  3442. * This should get us reasonable parallelism between CPU and GPU but also
  3443. * relatively low latency when blocking on a particular request to finish.
  3444. */
  3445. static int
  3446. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3447. {
  3448. struct drm_i915_private *dev_priv = dev->dev_private;
  3449. struct drm_i915_file_private *file_priv = file->driver_priv;
  3450. unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
  3451. struct drm_i915_gem_request *request, *target = NULL;
  3452. int ret;
  3453. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  3454. if (ret)
  3455. return ret;
  3456. /* ABI: return -EIO if already wedged */
  3457. if (i915_terminally_wedged(&dev_priv->gpu_error))
  3458. return -EIO;
  3459. spin_lock(&file_priv->mm.lock);
  3460. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  3461. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3462. break;
  3463. /*
  3464. * Note that the request might not have been submitted yet.
  3465. * In which case emitted_jiffies will be zero.
  3466. */
  3467. if (!request->emitted_jiffies)
  3468. continue;
  3469. target = request;
  3470. }
  3471. if (target)
  3472. i915_gem_request_reference(target);
  3473. spin_unlock(&file_priv->mm.lock);
  3474. if (target == NULL)
  3475. return 0;
  3476. ret = __i915_wait_request(target, true, NULL, NULL);
  3477. if (ret == 0)
  3478. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  3479. i915_gem_request_unreference(target);
  3480. return ret;
  3481. }
  3482. static bool
  3483. i915_vma_misplaced(struct i915_vma *vma, uint32_t alignment, uint64_t flags)
  3484. {
  3485. struct drm_i915_gem_object *obj = vma->obj;
  3486. if (alignment &&
  3487. vma->node.start & (alignment - 1))
  3488. return true;
  3489. if (flags & PIN_MAPPABLE && !obj->map_and_fenceable)
  3490. return true;
  3491. if (flags & PIN_OFFSET_BIAS &&
  3492. vma->node.start < (flags & PIN_OFFSET_MASK))
  3493. return true;
  3494. if (flags & PIN_OFFSET_FIXED &&
  3495. vma->node.start != (flags & PIN_OFFSET_MASK))
  3496. return true;
  3497. return false;
  3498. }
  3499. void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
  3500. {
  3501. struct drm_i915_gem_object *obj = vma->obj;
  3502. bool mappable, fenceable;
  3503. u32 fence_size, fence_alignment;
  3504. fence_size = i915_gem_get_gtt_size(obj->base.dev,
  3505. obj->base.size,
  3506. obj->tiling_mode);
  3507. fence_alignment = i915_gem_get_gtt_alignment(obj->base.dev,
  3508. obj->base.size,
  3509. obj->tiling_mode,
  3510. true);
  3511. fenceable = (vma->node.size == fence_size &&
  3512. (vma->node.start & (fence_alignment - 1)) == 0);
  3513. mappable = (vma->node.start + fence_size <=
  3514. to_i915(obj->base.dev)->ggtt.mappable_end);
  3515. obj->map_and_fenceable = mappable && fenceable;
  3516. }
  3517. static int
  3518. i915_gem_object_do_pin(struct drm_i915_gem_object *obj,
  3519. struct i915_address_space *vm,
  3520. const struct i915_ggtt_view *ggtt_view,
  3521. uint32_t alignment,
  3522. uint64_t flags)
  3523. {
  3524. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3525. struct i915_vma *vma;
  3526. unsigned bound;
  3527. int ret;
  3528. if (WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base))
  3529. return -ENODEV;
  3530. if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
  3531. return -EINVAL;
  3532. if (WARN_ON((flags & (PIN_MAPPABLE | PIN_GLOBAL)) == PIN_MAPPABLE))
  3533. return -EINVAL;
  3534. if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
  3535. return -EINVAL;
  3536. vma = ggtt_view ? i915_gem_obj_to_ggtt_view(obj, ggtt_view) :
  3537. i915_gem_obj_to_vma(obj, vm);
  3538. if (vma) {
  3539. if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  3540. return -EBUSY;
  3541. if (i915_vma_misplaced(vma, alignment, flags)) {
  3542. WARN(vma->pin_count,
  3543. "bo is already pinned in %s with incorrect alignment:"
  3544. " offset=%08x %08x, req.alignment=%x, req.map_and_fenceable=%d,"
  3545. " obj->map_and_fenceable=%d\n",
  3546. ggtt_view ? "ggtt" : "ppgtt",
  3547. upper_32_bits(vma->node.start),
  3548. lower_32_bits(vma->node.start),
  3549. alignment,
  3550. !!(flags & PIN_MAPPABLE),
  3551. obj->map_and_fenceable);
  3552. ret = i915_vma_unbind(vma);
  3553. if (ret)
  3554. return ret;
  3555. vma = NULL;
  3556. }
  3557. }
  3558. bound = vma ? vma->bound : 0;
  3559. if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
  3560. vma = i915_gem_object_bind_to_vm(obj, vm, ggtt_view, alignment,
  3561. flags);
  3562. if (IS_ERR(vma))
  3563. return PTR_ERR(vma);
  3564. } else {
  3565. ret = i915_vma_bind(vma, obj->cache_level, flags);
  3566. if (ret)
  3567. return ret;
  3568. }
  3569. if (ggtt_view && ggtt_view->type == I915_GGTT_VIEW_NORMAL &&
  3570. (bound ^ vma->bound) & GLOBAL_BIND) {
  3571. __i915_vma_set_map_and_fenceable(vma);
  3572. WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
  3573. }
  3574. vma->pin_count++;
  3575. return 0;
  3576. }
  3577. int
  3578. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  3579. struct i915_address_space *vm,
  3580. uint32_t alignment,
  3581. uint64_t flags)
  3582. {
  3583. return i915_gem_object_do_pin(obj, vm,
  3584. i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL,
  3585. alignment, flags);
  3586. }
  3587. int
  3588. i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
  3589. const struct i915_ggtt_view *view,
  3590. uint32_t alignment,
  3591. uint64_t flags)
  3592. {
  3593. struct drm_device *dev = obj->base.dev;
  3594. struct drm_i915_private *dev_priv = to_i915(dev);
  3595. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  3596. BUG_ON(!view);
  3597. return i915_gem_object_do_pin(obj, &ggtt->base, view,
  3598. alignment, flags | PIN_GLOBAL);
  3599. }
  3600. void
  3601. i915_gem_object_ggtt_unpin_view(struct drm_i915_gem_object *obj,
  3602. const struct i915_ggtt_view *view)
  3603. {
  3604. struct i915_vma *vma = i915_gem_obj_to_ggtt_view(obj, view);
  3605. WARN_ON(vma->pin_count == 0);
  3606. WARN_ON(!i915_gem_obj_ggtt_bound_view(obj, view));
  3607. --vma->pin_count;
  3608. }
  3609. int
  3610. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3611. struct drm_file *file)
  3612. {
  3613. struct drm_i915_gem_busy *args = data;
  3614. struct drm_i915_gem_object *obj;
  3615. int ret;
  3616. ret = i915_mutex_lock_interruptible(dev);
  3617. if (ret)
  3618. return ret;
  3619. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  3620. if (&obj->base == NULL) {
  3621. ret = -ENOENT;
  3622. goto unlock;
  3623. }
  3624. /* Count all active objects as busy, even if they are currently not used
  3625. * by the gpu. Users of this interface expect objects to eventually
  3626. * become non-busy without any further actions, therefore emit any
  3627. * necessary flushes here.
  3628. */
  3629. ret = i915_gem_object_flush_active(obj);
  3630. if (ret)
  3631. goto unref;
  3632. args->busy = 0;
  3633. if (obj->active) {
  3634. int i;
  3635. for (i = 0; i < I915_NUM_ENGINES; i++) {
  3636. struct drm_i915_gem_request *req;
  3637. req = obj->last_read_req[i];
  3638. if (req)
  3639. args->busy |= 1 << (16 + req->engine->exec_id);
  3640. }
  3641. if (obj->last_write_req)
  3642. args->busy |= obj->last_write_req->engine->exec_id;
  3643. }
  3644. unref:
  3645. drm_gem_object_unreference(&obj->base);
  3646. unlock:
  3647. mutex_unlock(&dev->struct_mutex);
  3648. return ret;
  3649. }
  3650. int
  3651. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3652. struct drm_file *file_priv)
  3653. {
  3654. return i915_gem_ring_throttle(dev, file_priv);
  3655. }
  3656. int
  3657. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3658. struct drm_file *file_priv)
  3659. {
  3660. struct drm_i915_private *dev_priv = dev->dev_private;
  3661. struct drm_i915_gem_madvise *args = data;
  3662. struct drm_i915_gem_object *obj;
  3663. int ret;
  3664. switch (args->madv) {
  3665. case I915_MADV_DONTNEED:
  3666. case I915_MADV_WILLNEED:
  3667. break;
  3668. default:
  3669. return -EINVAL;
  3670. }
  3671. ret = i915_mutex_lock_interruptible(dev);
  3672. if (ret)
  3673. return ret;
  3674. obj = to_intel_bo(drm_gem_object_lookup(file_priv, args->handle));
  3675. if (&obj->base == NULL) {
  3676. ret = -ENOENT;
  3677. goto unlock;
  3678. }
  3679. if (i915_gem_obj_is_pinned(obj)) {
  3680. ret = -EINVAL;
  3681. goto out;
  3682. }
  3683. if (obj->pages &&
  3684. obj->tiling_mode != I915_TILING_NONE &&
  3685. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  3686. if (obj->madv == I915_MADV_WILLNEED)
  3687. i915_gem_object_unpin_pages(obj);
  3688. if (args->madv == I915_MADV_WILLNEED)
  3689. i915_gem_object_pin_pages(obj);
  3690. }
  3691. if (obj->madv != __I915_MADV_PURGED)
  3692. obj->madv = args->madv;
  3693. /* if the object is no longer attached, discard its backing storage */
  3694. if (obj->madv == I915_MADV_DONTNEED && obj->pages == NULL)
  3695. i915_gem_object_truncate(obj);
  3696. args->retained = obj->madv != __I915_MADV_PURGED;
  3697. out:
  3698. drm_gem_object_unreference(&obj->base);
  3699. unlock:
  3700. mutex_unlock(&dev->struct_mutex);
  3701. return ret;
  3702. }
  3703. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3704. const struct drm_i915_gem_object_ops *ops)
  3705. {
  3706. int i;
  3707. INIT_LIST_HEAD(&obj->global_list);
  3708. for (i = 0; i < I915_NUM_ENGINES; i++)
  3709. INIT_LIST_HEAD(&obj->engine_list[i]);
  3710. INIT_LIST_HEAD(&obj->obj_exec_link);
  3711. INIT_LIST_HEAD(&obj->vma_list);
  3712. INIT_LIST_HEAD(&obj->batch_pool_link);
  3713. obj->ops = ops;
  3714. obj->fence_reg = I915_FENCE_REG_NONE;
  3715. obj->madv = I915_MADV_WILLNEED;
  3716. i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
  3717. }
  3718. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3719. .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE,
  3720. .get_pages = i915_gem_object_get_pages_gtt,
  3721. .put_pages = i915_gem_object_put_pages_gtt,
  3722. };
  3723. struct drm_i915_gem_object *i915_gem_object_create(struct drm_device *dev,
  3724. size_t size)
  3725. {
  3726. struct drm_i915_gem_object *obj;
  3727. struct address_space *mapping;
  3728. gfp_t mask;
  3729. int ret;
  3730. obj = i915_gem_object_alloc(dev);
  3731. if (obj == NULL)
  3732. return ERR_PTR(-ENOMEM);
  3733. ret = drm_gem_object_init(dev, &obj->base, size);
  3734. if (ret)
  3735. goto fail;
  3736. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3737. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3738. /* 965gm cannot relocate objects above 4GiB. */
  3739. mask &= ~__GFP_HIGHMEM;
  3740. mask |= __GFP_DMA32;
  3741. }
  3742. mapping = file_inode(obj->base.filp)->i_mapping;
  3743. mapping_set_gfp_mask(mapping, mask);
  3744. i915_gem_object_init(obj, &i915_gem_object_ops);
  3745. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3746. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3747. if (HAS_LLC(dev)) {
  3748. /* On some devices, we can have the GPU use the LLC (the CPU
  3749. * cache) for about a 10% performance improvement
  3750. * compared to uncached. Graphics requests other than
  3751. * display scanout are coherent with the CPU in
  3752. * accessing this cache. This means in this mode we
  3753. * don't need to clflush on the CPU side, and on the
  3754. * GPU side we only need to flush internal caches to
  3755. * get data visible to the CPU.
  3756. *
  3757. * However, we maintain the display planes as UC, and so
  3758. * need to rebind when first used as such.
  3759. */
  3760. obj->cache_level = I915_CACHE_LLC;
  3761. } else
  3762. obj->cache_level = I915_CACHE_NONE;
  3763. trace_i915_gem_object_create(obj);
  3764. return obj;
  3765. fail:
  3766. i915_gem_object_free(obj);
  3767. return ERR_PTR(ret);
  3768. }
  3769. static bool discard_backing_storage(struct drm_i915_gem_object *obj)
  3770. {
  3771. /* If we are the last user of the backing storage (be it shmemfs
  3772. * pages or stolen etc), we know that the pages are going to be
  3773. * immediately released. In this case, we can then skip copying
  3774. * back the contents from the GPU.
  3775. */
  3776. if (obj->madv != I915_MADV_WILLNEED)
  3777. return false;
  3778. if (obj->base.filp == NULL)
  3779. return true;
  3780. /* At first glance, this looks racy, but then again so would be
  3781. * userspace racing mmap against close. However, the first external
  3782. * reference to the filp can only be obtained through the
  3783. * i915_gem_mmap_ioctl() which safeguards us against the user
  3784. * acquiring such a reference whilst we are in the middle of
  3785. * freeing the object.
  3786. */
  3787. return atomic_long_read(&obj->base.filp->f_count) == 1;
  3788. }
  3789. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3790. {
  3791. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3792. struct drm_device *dev = obj->base.dev;
  3793. struct drm_i915_private *dev_priv = dev->dev_private;
  3794. struct i915_vma *vma, *next;
  3795. intel_runtime_pm_get(dev_priv);
  3796. trace_i915_gem_object_destroy(obj);
  3797. list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
  3798. int ret;
  3799. vma->pin_count = 0;
  3800. ret = i915_vma_unbind(vma);
  3801. if (WARN_ON(ret == -ERESTARTSYS)) {
  3802. bool was_interruptible;
  3803. was_interruptible = dev_priv->mm.interruptible;
  3804. dev_priv->mm.interruptible = false;
  3805. WARN_ON(i915_vma_unbind(vma));
  3806. dev_priv->mm.interruptible = was_interruptible;
  3807. }
  3808. }
  3809. /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
  3810. * before progressing. */
  3811. if (obj->stolen)
  3812. i915_gem_object_unpin_pages(obj);
  3813. WARN_ON(obj->frontbuffer_bits);
  3814. if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
  3815. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
  3816. obj->tiling_mode != I915_TILING_NONE)
  3817. i915_gem_object_unpin_pages(obj);
  3818. if (WARN_ON(obj->pages_pin_count))
  3819. obj->pages_pin_count = 0;
  3820. if (discard_backing_storage(obj))
  3821. obj->madv = I915_MADV_DONTNEED;
  3822. i915_gem_object_put_pages(obj);
  3823. i915_gem_object_free_mmap_offset(obj);
  3824. BUG_ON(obj->pages);
  3825. if (obj->base.import_attach)
  3826. drm_prime_gem_destroy(&obj->base, NULL);
  3827. if (obj->ops->release)
  3828. obj->ops->release(obj);
  3829. drm_gem_object_release(&obj->base);
  3830. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  3831. kfree(obj->bit_17);
  3832. i915_gem_object_free(obj);
  3833. intel_runtime_pm_put(dev_priv);
  3834. }
  3835. struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
  3836. struct i915_address_space *vm)
  3837. {
  3838. struct i915_vma *vma;
  3839. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  3840. if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL &&
  3841. vma->vm == vm)
  3842. return vma;
  3843. }
  3844. return NULL;
  3845. }
  3846. struct i915_vma *i915_gem_obj_to_ggtt_view(struct drm_i915_gem_object *obj,
  3847. const struct i915_ggtt_view *view)
  3848. {
  3849. struct i915_vma *vma;
  3850. GEM_BUG_ON(!view);
  3851. list_for_each_entry(vma, &obj->vma_list, obj_link)
  3852. if (vma->is_ggtt && i915_ggtt_view_equal(&vma->ggtt_view, view))
  3853. return vma;
  3854. return NULL;
  3855. }
  3856. void i915_gem_vma_destroy(struct i915_vma *vma)
  3857. {
  3858. WARN_ON(vma->node.allocated);
  3859. /* Keep the vma as a placeholder in the execbuffer reservation lists */
  3860. if (!list_empty(&vma->exec_list))
  3861. return;
  3862. if (!vma->is_ggtt)
  3863. i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
  3864. list_del(&vma->obj_link);
  3865. kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
  3866. }
  3867. static void
  3868. i915_gem_stop_engines(struct drm_device *dev)
  3869. {
  3870. struct drm_i915_private *dev_priv = dev->dev_private;
  3871. struct intel_engine_cs *engine;
  3872. for_each_engine(engine, dev_priv)
  3873. dev_priv->gt.stop_engine(engine);
  3874. }
  3875. int
  3876. i915_gem_suspend(struct drm_device *dev)
  3877. {
  3878. struct drm_i915_private *dev_priv = dev->dev_private;
  3879. int ret = 0;
  3880. mutex_lock(&dev->struct_mutex);
  3881. ret = i915_gpu_idle(dev);
  3882. if (ret)
  3883. goto err;
  3884. i915_gem_retire_requests(dev_priv);
  3885. i915_gem_stop_engines(dev);
  3886. i915_gem_context_lost(dev_priv);
  3887. mutex_unlock(&dev->struct_mutex);
  3888. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  3889. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3890. flush_delayed_work(&dev_priv->mm.idle_work);
  3891. /* Assert that we sucessfully flushed all the work and
  3892. * reset the GPU back to its idle, low power state.
  3893. */
  3894. WARN_ON(dev_priv->mm.busy);
  3895. return 0;
  3896. err:
  3897. mutex_unlock(&dev->struct_mutex);
  3898. return ret;
  3899. }
  3900. void i915_gem_init_swizzling(struct drm_device *dev)
  3901. {
  3902. struct drm_i915_private *dev_priv = dev->dev_private;
  3903. if (INTEL_INFO(dev)->gen < 5 ||
  3904. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3905. return;
  3906. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3907. DISP_TILE_SURFACE_SWIZZLING);
  3908. if (IS_GEN5(dev))
  3909. return;
  3910. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3911. if (IS_GEN6(dev))
  3912. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3913. else if (IS_GEN7(dev))
  3914. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3915. else if (IS_GEN8(dev))
  3916. I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
  3917. else
  3918. BUG();
  3919. }
  3920. static void init_unused_ring(struct drm_device *dev, u32 base)
  3921. {
  3922. struct drm_i915_private *dev_priv = dev->dev_private;
  3923. I915_WRITE(RING_CTL(base), 0);
  3924. I915_WRITE(RING_HEAD(base), 0);
  3925. I915_WRITE(RING_TAIL(base), 0);
  3926. I915_WRITE(RING_START(base), 0);
  3927. }
  3928. static void init_unused_rings(struct drm_device *dev)
  3929. {
  3930. if (IS_I830(dev)) {
  3931. init_unused_ring(dev, PRB1_BASE);
  3932. init_unused_ring(dev, SRB0_BASE);
  3933. init_unused_ring(dev, SRB1_BASE);
  3934. init_unused_ring(dev, SRB2_BASE);
  3935. init_unused_ring(dev, SRB3_BASE);
  3936. } else if (IS_GEN2(dev)) {
  3937. init_unused_ring(dev, SRB0_BASE);
  3938. init_unused_ring(dev, SRB1_BASE);
  3939. } else if (IS_GEN3(dev)) {
  3940. init_unused_ring(dev, PRB1_BASE);
  3941. init_unused_ring(dev, PRB2_BASE);
  3942. }
  3943. }
  3944. int i915_gem_init_engines(struct drm_device *dev)
  3945. {
  3946. struct drm_i915_private *dev_priv = dev->dev_private;
  3947. int ret;
  3948. ret = intel_init_render_ring_buffer(dev);
  3949. if (ret)
  3950. return ret;
  3951. if (HAS_BSD(dev)) {
  3952. ret = intel_init_bsd_ring_buffer(dev);
  3953. if (ret)
  3954. goto cleanup_render_ring;
  3955. }
  3956. if (HAS_BLT(dev)) {
  3957. ret = intel_init_blt_ring_buffer(dev);
  3958. if (ret)
  3959. goto cleanup_bsd_ring;
  3960. }
  3961. if (HAS_VEBOX(dev)) {
  3962. ret = intel_init_vebox_ring_buffer(dev);
  3963. if (ret)
  3964. goto cleanup_blt_ring;
  3965. }
  3966. if (HAS_BSD2(dev)) {
  3967. ret = intel_init_bsd2_ring_buffer(dev);
  3968. if (ret)
  3969. goto cleanup_vebox_ring;
  3970. }
  3971. return 0;
  3972. cleanup_vebox_ring:
  3973. intel_cleanup_engine(&dev_priv->engine[VECS]);
  3974. cleanup_blt_ring:
  3975. intel_cleanup_engine(&dev_priv->engine[BCS]);
  3976. cleanup_bsd_ring:
  3977. intel_cleanup_engine(&dev_priv->engine[VCS]);
  3978. cleanup_render_ring:
  3979. intel_cleanup_engine(&dev_priv->engine[RCS]);
  3980. return ret;
  3981. }
  3982. int
  3983. i915_gem_init_hw(struct drm_device *dev)
  3984. {
  3985. struct drm_i915_private *dev_priv = dev->dev_private;
  3986. struct intel_engine_cs *engine;
  3987. int ret;
  3988. /* Double layer security blanket, see i915_gem_init() */
  3989. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3990. if (HAS_EDRAM(dev) && INTEL_GEN(dev_priv) < 9)
  3991. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  3992. if (IS_HASWELL(dev))
  3993. I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
  3994. LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  3995. if (HAS_PCH_NOP(dev)) {
  3996. if (IS_IVYBRIDGE(dev)) {
  3997. u32 temp = I915_READ(GEN7_MSG_CTL);
  3998. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  3999. I915_WRITE(GEN7_MSG_CTL, temp);
  4000. } else if (INTEL_INFO(dev)->gen >= 7) {
  4001. u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
  4002. temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4003. I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
  4004. }
  4005. }
  4006. i915_gem_init_swizzling(dev);
  4007. /*
  4008. * At least 830 can leave some of the unused rings
  4009. * "active" (ie. head != tail) after resume which
  4010. * will prevent c3 entry. Makes sure all unused rings
  4011. * are totally idle.
  4012. */
  4013. init_unused_rings(dev);
  4014. BUG_ON(!dev_priv->kernel_context);
  4015. ret = i915_ppgtt_init_hw(dev);
  4016. if (ret) {
  4017. DRM_ERROR("PPGTT enable HW failed %d\n", ret);
  4018. goto out;
  4019. }
  4020. /* Need to do basic initialisation of all rings first: */
  4021. for_each_engine(engine, dev_priv) {
  4022. ret = engine->init_hw(engine);
  4023. if (ret)
  4024. goto out;
  4025. }
  4026. intel_mocs_init_l3cc_table(dev);
  4027. /* We can't enable contexts until all firmware is loaded */
  4028. if (HAS_GUC(dev)) {
  4029. ret = intel_guc_setup(dev);
  4030. if (ret)
  4031. goto out;
  4032. }
  4033. /*
  4034. * Increment the next seqno by 0x100 so we have a visible break
  4035. * on re-initialisation
  4036. */
  4037. ret = i915_gem_set_seqno(dev, dev_priv->next_seqno+0x100);
  4038. out:
  4039. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4040. return ret;
  4041. }
  4042. int i915_gem_init(struct drm_device *dev)
  4043. {
  4044. struct drm_i915_private *dev_priv = dev->dev_private;
  4045. int ret;
  4046. mutex_lock(&dev->struct_mutex);
  4047. if (!i915.enable_execlists) {
  4048. dev_priv->gt.execbuf_submit = i915_gem_ringbuffer_submission;
  4049. dev_priv->gt.init_engines = i915_gem_init_engines;
  4050. dev_priv->gt.cleanup_engine = intel_cleanup_engine;
  4051. dev_priv->gt.stop_engine = intel_stop_engine;
  4052. } else {
  4053. dev_priv->gt.execbuf_submit = intel_execlists_submission;
  4054. dev_priv->gt.init_engines = intel_logical_rings_init;
  4055. dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
  4056. dev_priv->gt.stop_engine = intel_logical_ring_stop;
  4057. }
  4058. /* This is just a security blanket to placate dragons.
  4059. * On some systems, we very sporadically observe that the first TLBs
  4060. * used by the CS may be stale, despite us poking the TLB reset. If
  4061. * we hold the forcewake during initialisation these problems
  4062. * just magically go away.
  4063. */
  4064. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4065. i915_gem_init_userptr(dev_priv);
  4066. i915_gem_init_ggtt(dev);
  4067. ret = i915_gem_context_init(dev);
  4068. if (ret)
  4069. goto out_unlock;
  4070. ret = dev_priv->gt.init_engines(dev);
  4071. if (ret)
  4072. goto out_unlock;
  4073. ret = i915_gem_init_hw(dev);
  4074. if (ret == -EIO) {
  4075. /* Allow ring initialisation to fail by marking the GPU as
  4076. * wedged. But we only want to do this where the GPU is angry,
  4077. * for all other failure, such as an allocation failure, bail.
  4078. */
  4079. DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
  4080. atomic_or(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
  4081. ret = 0;
  4082. }
  4083. out_unlock:
  4084. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4085. mutex_unlock(&dev->struct_mutex);
  4086. return ret;
  4087. }
  4088. void
  4089. i915_gem_cleanup_engines(struct drm_device *dev)
  4090. {
  4091. struct drm_i915_private *dev_priv = dev->dev_private;
  4092. struct intel_engine_cs *engine;
  4093. for_each_engine(engine, dev_priv)
  4094. dev_priv->gt.cleanup_engine(engine);
  4095. }
  4096. static void
  4097. init_engine_lists(struct intel_engine_cs *engine)
  4098. {
  4099. INIT_LIST_HEAD(&engine->active_list);
  4100. INIT_LIST_HEAD(&engine->request_list);
  4101. }
  4102. void
  4103. i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
  4104. {
  4105. struct drm_device *dev = dev_priv->dev;
  4106. if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
  4107. !IS_CHERRYVIEW(dev_priv))
  4108. dev_priv->num_fence_regs = 32;
  4109. else if (INTEL_INFO(dev_priv)->gen >= 4 || IS_I945G(dev_priv) ||
  4110. IS_I945GM(dev_priv) || IS_G33(dev_priv))
  4111. dev_priv->num_fence_regs = 16;
  4112. else
  4113. dev_priv->num_fence_regs = 8;
  4114. if (intel_vgpu_active(dev_priv))
  4115. dev_priv->num_fence_regs =
  4116. I915_READ(vgtif_reg(avail_rs.fence_num));
  4117. /* Initialize fence registers to zero */
  4118. i915_gem_restore_fences(dev);
  4119. i915_gem_detect_bit_6_swizzle(dev);
  4120. }
  4121. void
  4122. i915_gem_load_init(struct drm_device *dev)
  4123. {
  4124. struct drm_i915_private *dev_priv = dev->dev_private;
  4125. int i;
  4126. dev_priv->objects =
  4127. kmem_cache_create("i915_gem_object",
  4128. sizeof(struct drm_i915_gem_object), 0,
  4129. SLAB_HWCACHE_ALIGN,
  4130. NULL);
  4131. dev_priv->vmas =
  4132. kmem_cache_create("i915_gem_vma",
  4133. sizeof(struct i915_vma), 0,
  4134. SLAB_HWCACHE_ALIGN,
  4135. NULL);
  4136. dev_priv->requests =
  4137. kmem_cache_create("i915_gem_request",
  4138. sizeof(struct drm_i915_gem_request), 0,
  4139. SLAB_HWCACHE_ALIGN,
  4140. NULL);
  4141. INIT_LIST_HEAD(&dev_priv->vm_list);
  4142. INIT_LIST_HEAD(&dev_priv->context_list);
  4143. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  4144. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  4145. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4146. for (i = 0; i < I915_NUM_ENGINES; i++)
  4147. init_engine_lists(&dev_priv->engine[i]);
  4148. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  4149. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  4150. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  4151. i915_gem_retire_work_handler);
  4152. INIT_DELAYED_WORK(&dev_priv->mm.idle_work,
  4153. i915_gem_idle_work_handler);
  4154. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  4155. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  4156. /*
  4157. * Set initial sequence number for requests.
  4158. * Using this number allows the wraparound to happen early,
  4159. * catching any obvious problems.
  4160. */
  4161. dev_priv->next_seqno = ((u32)~0 - 0x1100);
  4162. dev_priv->last_seqno = ((u32)~0 - 0x1101);
  4163. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4164. init_waitqueue_head(&dev_priv->pending_flip_queue);
  4165. dev_priv->mm.interruptible = true;
  4166. mutex_init(&dev_priv->fb_tracking.lock);
  4167. }
  4168. void i915_gem_load_cleanup(struct drm_device *dev)
  4169. {
  4170. struct drm_i915_private *dev_priv = to_i915(dev);
  4171. kmem_cache_destroy(dev_priv->requests);
  4172. kmem_cache_destroy(dev_priv->vmas);
  4173. kmem_cache_destroy(dev_priv->objects);
  4174. }
  4175. int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
  4176. {
  4177. struct drm_i915_gem_object *obj;
  4178. /* Called just before we write the hibernation image.
  4179. *
  4180. * We need to update the domain tracking to reflect that the CPU
  4181. * will be accessing all the pages to create and restore from the
  4182. * hibernation, and so upon restoration those pages will be in the
  4183. * CPU domain.
  4184. *
  4185. * To make sure the hibernation image contains the latest state,
  4186. * we update that state just before writing out the image.
  4187. */
  4188. list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) {
  4189. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  4190. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  4191. }
  4192. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  4193. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  4194. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  4195. }
  4196. return 0;
  4197. }
  4198. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  4199. {
  4200. struct drm_i915_file_private *file_priv = file->driver_priv;
  4201. /* Clean up our request list when the client is going away, so that
  4202. * later retire_requests won't dereference our soon-to-be-gone
  4203. * file_priv.
  4204. */
  4205. spin_lock(&file_priv->mm.lock);
  4206. while (!list_empty(&file_priv->mm.request_list)) {
  4207. struct drm_i915_gem_request *request;
  4208. request = list_first_entry(&file_priv->mm.request_list,
  4209. struct drm_i915_gem_request,
  4210. client_list);
  4211. list_del(&request->client_list);
  4212. request->file_priv = NULL;
  4213. }
  4214. spin_unlock(&file_priv->mm.lock);
  4215. if (!list_empty(&file_priv->rps.link)) {
  4216. spin_lock(&to_i915(dev)->rps.client_lock);
  4217. list_del(&file_priv->rps.link);
  4218. spin_unlock(&to_i915(dev)->rps.client_lock);
  4219. }
  4220. }
  4221. int i915_gem_open(struct drm_device *dev, struct drm_file *file)
  4222. {
  4223. struct drm_i915_file_private *file_priv;
  4224. int ret;
  4225. DRM_DEBUG_DRIVER("\n");
  4226. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  4227. if (!file_priv)
  4228. return -ENOMEM;
  4229. file->driver_priv = file_priv;
  4230. file_priv->dev_priv = dev->dev_private;
  4231. file_priv->file = file;
  4232. INIT_LIST_HEAD(&file_priv->rps.link);
  4233. spin_lock_init(&file_priv->mm.lock);
  4234. INIT_LIST_HEAD(&file_priv->mm.request_list);
  4235. file_priv->bsd_ring = -1;
  4236. ret = i915_gem_context_open(dev, file);
  4237. if (ret)
  4238. kfree(file_priv);
  4239. return ret;
  4240. }
  4241. /**
  4242. * i915_gem_track_fb - update frontbuffer tracking
  4243. * @old: current GEM buffer for the frontbuffer slots
  4244. * @new: new GEM buffer for the frontbuffer slots
  4245. * @frontbuffer_bits: bitmask of frontbuffer slots
  4246. *
  4247. * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
  4248. * from @old and setting them in @new. Both @old and @new can be NULL.
  4249. */
  4250. void i915_gem_track_fb(struct drm_i915_gem_object *old,
  4251. struct drm_i915_gem_object *new,
  4252. unsigned frontbuffer_bits)
  4253. {
  4254. if (old) {
  4255. WARN_ON(!mutex_is_locked(&old->base.dev->struct_mutex));
  4256. WARN_ON(!(old->frontbuffer_bits & frontbuffer_bits));
  4257. old->frontbuffer_bits &= ~frontbuffer_bits;
  4258. }
  4259. if (new) {
  4260. WARN_ON(!mutex_is_locked(&new->base.dev->struct_mutex));
  4261. WARN_ON(new->frontbuffer_bits & frontbuffer_bits);
  4262. new->frontbuffer_bits |= frontbuffer_bits;
  4263. }
  4264. }
  4265. /* All the new VM stuff */
  4266. u64 i915_gem_obj_offset(struct drm_i915_gem_object *o,
  4267. struct i915_address_space *vm)
  4268. {
  4269. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  4270. struct i915_vma *vma;
  4271. WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
  4272. list_for_each_entry(vma, &o->vma_list, obj_link) {
  4273. if (vma->is_ggtt &&
  4274. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  4275. continue;
  4276. if (vma->vm == vm)
  4277. return vma->node.start;
  4278. }
  4279. WARN(1, "%s vma for this object not found.\n",
  4280. i915_is_ggtt(vm) ? "global" : "ppgtt");
  4281. return -1;
  4282. }
  4283. u64 i915_gem_obj_ggtt_offset_view(struct drm_i915_gem_object *o,
  4284. const struct i915_ggtt_view *view)
  4285. {
  4286. struct i915_vma *vma;
  4287. list_for_each_entry(vma, &o->vma_list, obj_link)
  4288. if (vma->is_ggtt && i915_ggtt_view_equal(&vma->ggtt_view, view))
  4289. return vma->node.start;
  4290. WARN(1, "global vma for this object not found. (view=%u)\n", view->type);
  4291. return -1;
  4292. }
  4293. bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
  4294. struct i915_address_space *vm)
  4295. {
  4296. struct i915_vma *vma;
  4297. list_for_each_entry(vma, &o->vma_list, obj_link) {
  4298. if (vma->is_ggtt &&
  4299. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  4300. continue;
  4301. if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
  4302. return true;
  4303. }
  4304. return false;
  4305. }
  4306. bool i915_gem_obj_ggtt_bound_view(struct drm_i915_gem_object *o,
  4307. const struct i915_ggtt_view *view)
  4308. {
  4309. struct i915_vma *vma;
  4310. list_for_each_entry(vma, &o->vma_list, obj_link)
  4311. if (vma->is_ggtt &&
  4312. i915_ggtt_view_equal(&vma->ggtt_view, view) &&
  4313. drm_mm_node_allocated(&vma->node))
  4314. return true;
  4315. return false;
  4316. }
  4317. bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
  4318. {
  4319. struct i915_vma *vma;
  4320. list_for_each_entry(vma, &o->vma_list, obj_link)
  4321. if (drm_mm_node_allocated(&vma->node))
  4322. return true;
  4323. return false;
  4324. }
  4325. unsigned long i915_gem_obj_ggtt_size(struct drm_i915_gem_object *o)
  4326. {
  4327. struct i915_vma *vma;
  4328. GEM_BUG_ON(list_empty(&o->vma_list));
  4329. list_for_each_entry(vma, &o->vma_list, obj_link) {
  4330. if (vma->is_ggtt &&
  4331. vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
  4332. return vma->node.size;
  4333. }
  4334. return 0;
  4335. }
  4336. bool i915_gem_obj_is_pinned(struct drm_i915_gem_object *obj)
  4337. {
  4338. struct i915_vma *vma;
  4339. list_for_each_entry(vma, &obj->vma_list, obj_link)
  4340. if (vma->pin_count > 0)
  4341. return true;
  4342. return false;
  4343. }
  4344. /* Like i915_gem_object_get_page(), but mark the returned page dirty */
  4345. struct page *
  4346. i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, int n)
  4347. {
  4348. struct page *page;
  4349. /* Only default objects have per-page dirty tracking */
  4350. if (WARN_ON((obj->ops->flags & I915_GEM_OBJECT_HAS_STRUCT_PAGE) == 0))
  4351. return NULL;
  4352. page = i915_gem_object_get_page(obj, n);
  4353. set_page_dirty(page);
  4354. return page;
  4355. }
  4356. /* Allocate a new GEM object and fill it with the supplied data */
  4357. struct drm_i915_gem_object *
  4358. i915_gem_object_create_from_data(struct drm_device *dev,
  4359. const void *data, size_t size)
  4360. {
  4361. struct drm_i915_gem_object *obj;
  4362. struct sg_table *sg;
  4363. size_t bytes;
  4364. int ret;
  4365. obj = i915_gem_object_create(dev, round_up(size, PAGE_SIZE));
  4366. if (IS_ERR(obj))
  4367. return obj;
  4368. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  4369. if (ret)
  4370. goto fail;
  4371. ret = i915_gem_object_get_pages(obj);
  4372. if (ret)
  4373. goto fail;
  4374. i915_gem_object_pin_pages(obj);
  4375. sg = obj->pages;
  4376. bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
  4377. obj->dirty = 1; /* Backing store is now out of date */
  4378. i915_gem_object_unpin_pages(obj);
  4379. if (WARN_ON(bytes != size)) {
  4380. DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
  4381. ret = -EFAULT;
  4382. goto fail;
  4383. }
  4384. return obj;
  4385. fail:
  4386. drm_gem_object_unreference(&obj->base);
  4387. return ERR_PTR(ret);
  4388. }